我有以下之後$手錶回調執行:
$scope.$watch('duration.dayPreference',function(value){
console.log(value);
if(value=='every')
{
that.duration.days = 1;
}
else if(value=='selected')
{
//alert('test');
that.duration.days=[];
}
else if(value=='everyday')
{
that.duration.days='everyday';
}
});
this.selectDay = function (day) {
$scope.duration.dayPreference = 'selected';
//$scope.$apply();
/*if(typeof(this.duration.days)!='object')
{
this.duration.days=[];
}*/
var index = this.duration.days.indexOf(day);
if (index == -1) {
//alert('test2');
this.duration.days.push(day);
}
else {
this.duration.days.splice(index, 1);
}
}
在此,當我做$scope.duration.dayPreference = 'selected';
我預計線下之有this.duration.days設置爲空陣列。但事實並非如此。經仔細檢查,我發現$ watch中的回調在分配下方的行之後運行。
$ watch可能很有可能在內部使用某些定時器。那麼應該怎麼做呢。
(請注意,'that.duration.days'陣列,'selectedDays'創建一次,而不是重新創建每次'dayPreference'變化相反,舊陣時「記住了。 「,並且將包含您放入其中的任何內容。) – DRobinson