Javascript Angularjs绑定数组元素ng repeat in指令

Javascript Angularjs绑定数组元素ng repeat in指令,javascript,arrays,angularjs,directive,Javascript,Arrays,Angularjs,Directive,我试图使用ng REPLATE和一个指令显示数组的元素。指令部分对解决方案很重要。但是,数组的元素未被绑定,并显示空值 小提琴可以在 HTML 预期产量 ["Anencephalous","Borborygm","Collywobbles"] •Anencephalous •Borborygm •Collywobbles 感谢您的帮助您没有绑定word 您使用了隔离作用域。如果不绑定它的scope属性,它将无法工作 scope: { word: '=' }, 像这样试试 <

我试图使用ng REPLATE和一个指令显示数组的元素。指令部分对解决方案很重要。但是,数组的元素未被绑定,并显示空值

小提琴可以在

HTML

预期产量

["Anencephalous","Borborygm","Collywobbles"]
•Anencephalous
•Borborygm
•Collywobbles   

感谢您的帮助

您没有绑定
word

您使用了隔离作用域。如果不绑定它的
scope
属性,它将无法工作

scope: {
    word: '='
},
像这样试试

<test word="word" ng-repeat="word in chat.words"></test>

var-app=angular.module('dr',[]);
app.controller(“testCtrl”,函数($scope){
$scope.chat={单词:[
“无脑儿的”、“Borborygm的”、“Collywobbles的”
]};
});
应用指令('测试',函数(){
返回{
限制:“EA”,
范围:{
字:'='
},
优先权:1001,
模板:“
  • {{word}
  • ”, 替换:正确, 链接:函数(范围、elm、属性){ } } });
    您的指令需要在ng repeat之前使用更高的优先级运行,因此当ng repeat克隆元素时,它能够选择您的修改

    中的“编译/链接分离背后的原因”一节对此进行了解释


    当前ng repeat优先级为1000,因此任何高于此优先级的操作都可以。

    请您对代码进行解释?为什么这能解决问题?
    ["Anencephalous","Borborygm","Collywobbles"]
    •Anencephalous
    •Borborygm
    •Collywobbles   
    
    scope: {
        word: '='
    },
    
    <test word="word" ng-repeat="word in chat.words"></test>
    
    var app = angular.module('dr', []);
    app.controller("testCtrl", function($scope) {
        $scope.chat= {words: [
          'Anencephalous', 'Borborygm', 'Collywobbles'
        ]};
    });
    app.directive('test', function() {
        return {
            restrict: 'EA',
            scope: {
                word: '='
            },
            priority: 1001,
            template: "<li>{{word}}</li>",
            replace: true,        
            link: function(scope, elm, attrs) {             
            }
        }
    });