【问题标题】:Using directives in angularjs with templateUrl在带有 templateUrl 的 angularjs 中使用指令
【发布时间】:2015-11-11 08:01:23
【问题描述】:

我用 angularjs 编写了一个代码,该代码使用指令从 show-category.html 文件中获取类别列表并将它们显示在索引页面上,我按照我所学的做了所有事情,但仍然无法获取类别加载 index.html 时显示。

在 app.js 文件中

app.directive('showCategories', function() {
    return {
      restrict: 'E',
      templateUrl:  'show-categories.html'


    };
});

您可以在此处查看 plunker 上的完整代码: http://plnkr.co/edit/FSsNAq?p=preview

【问题讨论】:

    标签: angularjs angularjs-directive plunker angularjs-templates


    【解决方案1】:

    你把你的指令定义放在控制器的中间,把它带到外面,它就可以工作了(除非你在那里有一些其他不存在的功能):

    app.controller("BookCtrl", function($scope) {
    
      $scope.categories = [{
          "id": 0,
          "name": "Type"
        }, {
          "id": 1,
          "name": "Date"
        }, {
          "id": 1,
          "name": "Name"
        }
    
      ];
      ...
    
    });
    
    app.directive('showCategories', function() {
        return {
          restrict: 'E',
          templateUrl:  'show-categories.html'
    
    
        };
    });
    

    Plunker

    【讨论】:

    • 我不知道这个错误是怎么过去的,谢谢大佬!!我打算只显示必要的代码让您理解问题而不会被其他功能混淆,因为我的应用程序比这长得多..
    猜你喜欢
    • 2015-01-16
    • 1970-01-01
    • 2013-01-23
    • 2016-04-24
    • 2015-06-05
    • 1970-01-01
    • 2014-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多