0

<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();
            }
        }]);
}
4

1 回答 1

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');
                });
于 2013-07-01T17:30:58.253 回答