无法观察/观察范围?

时间:2013-07-01 17:09:08

标签: angularjs

<button-large color="green" click="createWorkstation()" busy="disableSave()" busyLabel="Saving...">Save</button-large>

我无法观察disableSave()输出的变化。当.busy的输出发生变化时,我的指令中显示的console.log()永远不会被触发。我做错了什么?

directive('buttonLarge', function () {
    return {
        scope: {
            busy: '&',
            click: '&'
        },
        replace: true,
        restrict: 'E',
        transclude: true,
        template: '<button class="buttonL" ng-transclude/>',
        link: function (scope, element, attrs) {

            //when the button is busy, disable the button
            if (angular.isDefined(scope.busy())) {
                scope.$watch(scope.busy(), function () {
                    console.log('watched');
                });
                attrs.$observe(scope.busy(), function () {
                    console.log('observed');
                });
            }

            //setup click event - https://groups.google.com/forum/#!topic/angular/-uVE5WJWwLA
            if (angular.isDefined(scope.click)) {
                element.bind('click', scope.click);
            }
        }
    }
})

控制器

$scope.newWorkstationDialog = function (workflowProcess) {
    var d = $dialog.
        dialog({
            resolve: {
                workflowProcess: function () {
                    return workflowProcess;
                }
            }
        }).
        open('/partials/admin/'+workflowProcess.entity.slug+'/setup.htm', ['$scope', 'dialog', ..., function ($scope, dialog, ...) {
            $scope.saving = false;

            /* Create the workstation */
            $scope.createWorkstation = function () {
                console.log('saving');
                $scope.saving = true;
                $timeout(function () {
                    $scope.saving = false;
                    console.log('stopped saving');
                }, 1000);
            }

            //Should the save button be disabled?
            $scope.disableSave = function () {
                return $scope.saving;//|| $scope.form.$valid;
            }

            $scope.cancel = function () {
                dialog.close();
            }
        }]);
}

1 个答案:

答案 0 :(得分:2)

你观看的语法不正确。你在观看时应该不使用范围因为内部它使用内部附加范围的$ parse服务。所以你需要修改你的代码,如下所示

 1st option 

 scope.$watch(function(){
return scope.busy()
}, function (newvalue,oldvalue) {
                    console.log('watched');
                });

2nd option

scope.$watch('busy()', function (newvalue,oldvalue) {
                    console.log('watched');
                });