1

ジャバスクリプト:

var App = angular.module('App', []);

App.controller('RootCntr', function ($scope) {
    $scope.openedShelf = false;

    console.log('controller', $scope.openedShelf);

    setTimeout(function() {
        $scope.openedShelf = true;
        console.log('controller', $scope.openedShelf);
    }, 2000);
});

App.directive('shelf', function () {
    return {
        restrict: 'E',
        scope: {
            'open': '='
        },
        link: function (scope, element, attrs) {
            console.log('linked');

            scope.$watch('open', function (newVal, oldVal) {
                console.log(newVal);
                if (newVal) {
                    console.log(true, newVal);
                }
            }, true);
        }
    };
});

HTML:

<body ng-app="App">
    <div ng-controller="RootCntr">
        <shelf open='openedShelf'></shelf>
    </div>
</body>

ディレクティブの値を変更するopenedShelfRootCntr、watch ステートメントで更新がキャッチされません。何か案は?

4

1 に答える 1