更改$ scope属性时$ watch不会触发

时间:2017-02-07 12:06:56

标签: angularjs angularjs-directive

我怀疑自己做错了什么或者我不理解。我的指令有什么问题?

app.directive('sideMenu', function() {
    return {
        restrict: 'E',
        templateUrl : '/app/components/sidemenu/side-menu.html',
        controller: ['$scope', function SideMenuController($scope) {
            $scope.sidebarState = false;

            this.toggleSideBar = function() {
                $scope.sidebarState = !$scope.sidebarState;
            };  
        }],
        controllerAs: "sideMenuCtrl",
        link: function(scope, elem, attr) {
            scope.$watch(attr.sidemenuDirective, function(newVal) {
                console.log(scope);
            }); 
        }
    };
});


<nav class="side-menu" sidemenu-directive="sidebarState">

<div class="side-menu-controls">

    <a class="active toggle-menu" href="#" ng-click="sideMenuCtrl.toggleSideBar()">
        <span class="hb">
            <em></em>
            <em></em>
            <em></em>
        </span>
    </a>
    <div class="clearfix"></div>

</div>

<div class="side-menu-inner" ng-controller="TasklistController as tasklistCtrl">

    <div class="items-menu">
        <ul>
            <li ng-class="{active: tasklistCtrl.isSelected(list)}" ng-repeat="list in taskLists">
                <a ng-click="tasklistCtrl.selectList(list)">{{list.name}}</a>
            </li>
        </ul>
    </div>

</div>

<body class="expanded">
    <side-menu></side-menu>
</body>

0 个答案:

没有答案