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

时间:2015-02-14 13:47:30

标签: angularjs angularjs-directive

我想在自定义指令中添加ng-change函数,但我不确定ng-change是否会进入模板或链接功能,此外我还想使ng-change可选(调用'

< text-edit-in-place value =" loan.due_date"变化=" checkException"  PARAM =" foobar的">

'将checkException('foobar')函数添加到省略更改=& 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;
            };
        }
    };
}

帮助表示赞赏,谢谢。

1 个答案:

答案 0 :(得分:2)

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

的格式

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

示例:

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

这应该是最直接的方式。

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