我遇到了一个让我疯狂的自定义指令的问题.我正在尝试创建以下自定义(属性)指令:
angular.module('componentes', [])
.directive("seatMap", function (){
return {
restrict: 'A',
link: function(scope, element, attrs, controller){
function updateSeatInfo(scope, element){
var txt = "";
for (var i in scope.seats)
txt = txt + scope.seats[i].id + " ";
$(element).text("seat ids: "+txt);
}
/*
// This is working, but it's kind of dirty...
$timeout(function(){updateSeatInfo(scope,element);}, 1000);
*/
scope.$watch('seats', function(newval, oldval){
console.log(newval, oldval);
updateSeatInfo(scope,element);
});
}
}
});
Run Code Online (Sandbox Code Playgroud)
这个"属性类型"指令(称为seatMap)试图显示我将从服务器通过$ resource service(参见下面的代码)获取到div(元素)的座位ID列表(例如,用于剧院) .
我正在使用这个简单的部分html:
<div>
<!-- This is actually working -->
<ul>
<li ng-repeat="seat in seats">{{seat.id}}</li>
</ul> …Run Code Online (Sandbox Code Playgroud)