AngularJs绑定在指令中延迟

时间:2015-07-28 15:11:41

标签: angularjs binding directive

请看小提琴:https://jsfiddle.net/ThiagoRomam/1hyguh6n/

$scope.setDates = function(initialDate, finalDate) {
    $scope.initialDate = initialDate;
    $scope.finalDate = finalDate;
    $scope.apply();
};

当您按下输入中的任意键或单击选项(所有时间,今天)时,将在完成绑定之前调用 apply 方法。

我该如何解决?

1 个答案:

答案 0 :(得分:1)

Add the $timeout to wait for the $digest to finish. Check working demo: JSFiddle

app.directive("dateFilter", ['$timeout', function ($timeout) {
    ...
    $timeout(function () {
        $scope.apply();
    }); 

Suggestion

Do not use function name like apply, in case mix with the built-in function $apply.