我的指令中有以下代碼。AngularJS:範圍變量不夠快地更新
//Directive Code
var BooleanWidgetController = function ($scope, $filter) {
$scope.booleanOptions = [
{
displayText: '-- ' + $filter('i18n')('Widgets.Generics.Select') + ' --'
},
{
value: 1,
displayText: $filter('i18n')('Widgets.Generics.Yes')
},
{
value: 0,
displayText: $filter('i18n')('Widgets.Generics.No')
}
];
//Added inside watch because query was not being updated if filterUpdated was called using ng-change
$scope.$watch('query', $scope.filterUpdated);
};
app.directive('acxBooleanColumnHeaderFilter', function() {
return {
restrict: 'A',
replace: true,
controller: ['$scope', '$filter', BooleanWidgetController],
scope: {
query: '=',
filterUpdated: '&submit',
columnHeading: '@'
},
templateUrl: 'mailSearch/directives/columnHeaderWidgets/boolean/booleanColumnHeaderWidget.tpl.html'
};
});
//Template
<div class="columnHeaderWidget">
<div class="title pull-left">{{columnHeading}}</div>
<div style="clear:both"></div>
<select ng-model="query" ng-options="option.value as option.displayText for option in booleanOptions">
</select>
目前的辦法是工作的罰款。但是當我嘗試做這樣的事情。
<select ng-model="query" ng-change="filterUpdated" ng-options="option.value as option.displayText for option in booleanOptions">
$ scope.query的更新速度不夠快。所以$ scope.query在$ scope.filterUpdated被調用後被更新。我在這裏錯過了什麼?
我的代碼已經worki如問題中提到的那樣,但是我無法理解的是爲什麼查詢範圍變量沒有足夠快地更新。如果我進行級聯調用,如$ scope.query = 0; $ scope.filterUpdated(); filterUpdated函數不會將$ scope.query設爲0,而$ scope.query將具有以前的值。 – 2014-09-25 07:08:45