Angularjs 通过ngRepeat在指令作用域数组上循环
在我的指令中,当我宣布Angularjs 通过ngRepeat在指令作用域数组上循环,angularjs,angularjs-directive,angularjs-scope,Angularjs,Angularjs Directive,Angularjs Scope,在我的指令中,当我宣布 scope.type = 'A'; 然后在html中,我可以通过代码访问A <p>{{type}}</p> 然后在html中,当我使用下面的ngRepeat时,我无法打印A和B <div ng-repeat="type in types"> <p>{{type}}</p> <input type="checkbox" id={{type}} ng-click="checkboxClick()"
scope.type = 'A';
然后在html中,我可以通过代码访问A
<p>{{type}}</p>
然后在html中,当我使用下面的ngRepeat时,我无法打印A
和B
<div ng-repeat="type in types">
<p>{{type}}</p>
<input type="checkbox" id={{type}} ng-click="checkboxClick()" checked>
</div>
你能发一些代码吗?你试过只打印带有的{{type}}而不打印标签吗?嗯……我的源代码太长了,让我想想如何为你简化它。你的指令在哪里?它声明了什么类型的作用域?假、真或孤立?它可能与
ng repeat
实例作用域发生冲突。您的示例的工作原理如下:@Ulydev a指令的链接函数不使用注入。link函数的第一个参数始终是scope。不管你叫它$scope
,scope
,还是someScope
。指令的控制器函数使用注入,在这种情况下,参数的名称很重要。但这不是问题所在,因为他在link函数中设置列表。
<div ng-repeat="type in types">
<p>{{type}}</p>
<input type="checkbox" id={{type}} ng-click="checkboxClick()" checked>
</div>
app.directive('chartDirective', function(socketio) {
'use strict';
return {
restrict: 'E', //E = element
templateUrl: '../../views/partials/chart.html',
controller: DataController,
controllerAs: 'dataCtrl',
link: function(scope, elem, attrs, ctrl) {
scope.Watch = scope.$watch(function() {
return ctrl.data;
}, function(newVal) {
if (newVal) {
//many irrelevant codes here
// test codes
scope.ty = 'A';
scope.types = ['A', 'B'];
//many irrelevant codes here
}
});
}
};
});