【问题标题】:array is returning in console but is not showing in the Html format in angularjs数组在控制台中返回,但未在 angularjs 中以 Html 格式显示
【发布时间】:2016-11-26 22:11:49
【问题描述】:

这是我的 App.js 文件,请仔细查看我做错了什么。我在下面提供了相关文件 index.html 和 founditemtemplate.html。它在控制台中返回对象数组,但未以格式显示我在founditemtemplate.index中提供的

(function () {
    'use strict'

    angular.module('NarrowItDownApp', [])
        .controller('NarrowItDownController', NarrowItDownController )
        .service('MenuSearchService', MenuSearchService)
        .directive('foundItems', FoundItemsDirective)
    ;

    function FoundItemsDirective() {
        var ddo = {
            templateUrl: 'foundItemTemplate.html',
            restrict: 'E',
            scope: {
                items: '<',
                onRemove: '&'
            }
        };
        return ddo;
    }

    NarrowItDownController.$inject = ['MenuSearchService']
    function NarrowItDownController(MenuSearchService) {
        var ctrlNarrow = this;
        ctrlNarrow.found = [];

        ctrlNarrow.searchItems = function () {
            ctrlNarrow.found = MenuSearchService.getMatchedMenuItems(ctrlNarrow.searchTerm);
        }

        ctrlNarrow.remove = function(index){
            ctrlNarrow.found.splice(index, 1);
        }
    }


    MenuSearchService.$inject = ['$http']
    function MenuSearchService($http) {

        var service = this;

        service.getMatchedMenuItems = function(searchTerm) {

            if (!service.data) service.getData();
            if (searchTerm === "") return [];

            var items = service.data.menu_items;
            var found = [];

            for (var i = 0; i < items.length; i++) {
                var desc = items[i].description;
                if (desc.indexOf(searchTerm) !== -1) {
                    found.push(items[i]);
                }
            }

            console.dir(found);
            return found;
        };

        service.getData = function () {

            $http({
                method: "GET",
                url: ("https://davids-restaurant.herokuapp.com/menu_items.json")
            }).then( function (result) {
                console.log(result.data);
                service.data = result.data;
            }, function (result) {
                console.log(result.data);
                service.getData();
            });
        }
        service.getData();
    }
})();

这是我的主要 Index.html 文件

<!doctype html>
<html lang="en" ng-app="NarrowItDownApp">
  <head>
    <title>Narrow Down Your Menu Choice</title>
      <script src="js/angular.min.js"></script>
      <script src="js/app.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="styles/bootstrap.min.css">
    <link rel="stylesheet" href="styles/styles.css">
  </head>
<body  ng-controller="NarrowItDownController as syntax">
   <div class="container">
    <h1>Narrow Down Your Chinese Menu Choice</h1>

    <div class="form-group">
      <input type="text" placeholder="search term" class="form-control" ng-model="syntax.searchTerm">
    </div>
    <div class="form-group narrow-button">
      <button class="btn btn-primary" ng-click="syntax.searchItems()">Narrow It Down For Me!</button>
    </div>

    <items-loader-indicator></items-loader-indicator>

    <!-- found-items should be implemented as a component -->
       <div>
            <found-items items="syntax.found" on-remove="syntax.remove(index)"></found-items>
       </div>
  </div>

</body>
</html>

这里是可能的 Founditemtemplate.html

<ul>
    <div>
        <li ng-repeat="item in items">
            <div>
                <b>Name:</b>({item.name})
            </div>
            <div>
                <b>Description:</b> {{item.description}}
            </div>
            <button ng-click="onRemove({index: $index});">Don't want this one!</button>
        </li>
    </div>
</ul>
<div ng-if="items.length == 0">Nothing Found</div>

【问题讨论】:

  • 你能提供一个工作的jsfiddle吗? jsfiddle.net
  • 不,我没有在 jsfiddle.net 上尝试过这个

标签: javascript html angularjs


【解决方案1】:

我将您的代码放在 Plunker - HERE 中,它工作正常。

你有没有可能弄乱foundItemTemplate.html文件名的大小写?

应该和这里一模一样

templateUrl: 'foundItemTemplate.html',

这是我唯一能想到的,它确实复制了你描述的行为。

【讨论】:

    猜你喜欢
    • 2021-06-21
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 2020-03-11
    相关资源
    最近更新 更多