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

前端 未结 5 778
没有蜡笔的小新
没有蜡笔的小新 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:38

    Use $scope.$apply() after changing the $scope.myValue in your controller like:

    testApp.controller('testCtrl', function ($scope) {
        $scope.myValue = '1';
        $scope.update = function () {
            // Expecting local variable k, or $scope.pkey to have been
            // updated by calls in the directive's scope.
            console.log('CTRL:', $scope.myValue);
            $scope.myValue = "2";
            $scope.$apply();
            console.log('CTRL:', $scope.myValue);
        };
    });
    

提交回复
热议问题