【发布时间】:2015-07-31 05:49:50
【问题描述】:
模板 html:
我在模板 seats.tpl.html 文件中有一个元素,如下所示:
<select id="guest_table">
<option tables="emptySeats"
ng-repeat="table in emptySeats" value="{{table.tableId}}">{{table.tableNumber}}</option>
</select>
控制器
进入控制器,我调用工厂从 dbase 获取值,实际上我将结果加载到 $scope.emptySeats 数组中,该数组使用 ng-repeat 创建模板:
factP3_GetEmptyChairs.getEmptyChairs({'data': {'id': 58}})
.then(function (result) {
**$scope.emptySeats = result.data;**
$log.info("Empty chairs list loaded.");
},
function (result) {
$('.loading').hide();
$log.error("Error: we could not get Empty seats list.");
});
指令:
在我的指令中,我使用参数 templateUrl 来调用模板文件以加载,到目前为止还不错,选择加载了值。
.directive('emptySeats',function(){
return {
restrict: 'AE',
replace: true,
scope:{tables :'='},
templateUrl: function(){
return 'assets/modules/part3/templates/emptySeats.tpl.html';
},
link:function(scope, element, attrs) {
//bind change on element
element.bind('change', function() {
alert("table changed");
});
}
}
我的问题是,每当我更改 $scope.emptySeats 数组值时,我都需要自动更新这些值,而我当前的代码不会发生这种情况。
我认为指令中遗漏了一些东西,它是否可以编译,是否可以观察,是否是 ngmodel,有人可以帮忙吗?
更新: 我调用一个函数从数据库中获取新数据并将它们加载到选择元素
$scope.onClickHall = function (hall) {
var tmp = [];
//call service to load empty Seats
factP3_GetEmptyChairs.getEmptyChairs({'data': {'id': $scope.currentHall}})
.then(function (result) {
//$scope.emptySeats = result.data;
答:如果我在这里使用 $apply,我会得到:错误:$rootScope:inprog 行动已在进行中
$scope.$apply(function () {
$scope.emptySeats = result.data;;
});
tmp = result.data; //i use a temp array to pass it on timeout
$log.info("Empty chairs list loaded.");
},
function (result) {
$('.loading').hide();
$log.error("Error: we could not get Empty seats list.");
});
B.如果我使用 $timeout 函数并尝试更改数据,则没有任何反应
$timeout(function() {
$scope.$apply(function () {
$scope.emptySeats = tmp;
});
}, 10);
}
【问题讨论】:
标签: javascript angularjs templates directive controllers