AngularJS: Parent scope is not updated in directive (with isolated scope) two way binding

前端 未结 5 783
没有蜡笔的小新
没有蜡笔的小新 2020-12-08 20:09

I have a directive with isolated scope with a value with two way binding to the parent scope. I am calling a method that changes the value in the parent scope, but the chang

5条回答
  •  爱一瞬间的悲伤
    2020-12-08 20:47

    Using $apply() like the accepted answer can cause all sorts of bugs and potential performance hits as well. Settings up broadcasts and whatnot is a lot of work for this. I found the simple workaround just to use the standard timeout to trigger the event in the next cycle (which will be immediately because of the timeout). Surround the parentUpdate() call like so:

    $timeout(function() {
        $scope.parentUpdate();
    });
    

    Works perfectly for me. (note: 0ms is the default timeout time when not specified)

提交回复
热议问题