AngularJS - ng-change 作为自定义指令中的一个选项

AngularJS - ng-change as an option in a custom directive

我想将 ng-change 函数添加到自定义指令中,但我不确定 ng-change 是否会进入模板或进入 link 函数,另外我会喜欢使 ng-change 可选(调用 '

<文本就地编辑值="loan.due_date" 更改="checkException" 参数="foobar">

' 将添加 checkException('foobar') 函数,其中省略 change= & param= 不会 运行 任何 ng-change).

function TextEditInPlaceDirective() {
    return {
        restrict: 'E',
        scope: {
            value: '='
        },
        template: '<span ng-click="edit()" ng-show="!editing">{{ value}}</span><input ng-model="value" ng-blur="onBlur()" ng-show="editing"></input>',
        link: function($scope, element, attrs) {
            var inputElement = element.find('input');

            // reference the input element
            element.addClass('edit-in-place');

            // Initially, we're not editing.
            $scope.editing = false;

            // ng-click handler to activate edit-in-place
            $scope.edit = function() {
                $scope.editing = true;

                // element not visible until digest complete
                // timeout causes this to run after digest
                setTimeout(function() {
                    inputElement[0].focus();
                });
            };

            $scope.onBlur = function() {
                $scope.editing = false;
            };
        }
    };
}

不胜感激,谢谢。

您应该能够使用类似于 This fiddle

的格式

在该格式中,您可以在指令中定义函数,然后指定是否要调用该函数。

示例:

<test-change value="blah" check-exceptions="true" ng-model="foo"/>

这应该是最直接的方法。

然后你可以在指令的link函数中定义函数。