为什么ng repeat不在angular js中工作(使用指令)

why ng-repeat not working in angular js (using directives)?

本文关键字:指令 工作 repeat ng 不在 angular js 为什么      更新时间:2023-09-26

我在自定义指令中使用ng repeat,但我遇到了错误。你能告诉我如何删除这个错误吗

这是我的plunkerhttp://plnkr.co/edit/uj8b3hL8T6MjoKSZyjsc?p=preview自定义指令//代码转到此处

angular.module('ui.directive',[]).directive('newDir',function(){
    return{
        restrict:'E',
        scope:{
            data:'='
        },
        replace:true,
        templateUrl:"pop.html",
        controller:function($scope){
           console.log($scope.data)
        },
        link :function(scope,element,attr){
            element.click(function(){

            })
        }
    }
})

生成的错误非常不言自明:

Error: [$compile:tplrt] Template for directive 'newDir' must have exactly one root element. pop.html

从本质上讲,您需要确保您的模板有一个根节点。您在末尾附加了一个br标记。

更改:

<div><h1>{{str.name}}</h1><p>{{str.category}}</p></div></br>

收件人:

<div><h1>{{str.name}}</h1><p>{{str.category}}</p></div>

此外,

您的模板引用的是str,但作用域变量是data。更改您的模板:

<div><h1>{{data.name}}</h1><p>{{data.category}}</p></div>

http://plnkr.co/edit/eUgmU45quL0VoNLY19ek?p=preview