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

标签 angularjs angularjs-directive

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

' 将添加 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"/>

这应该是最直接的方法。

然后您可以在指令的链接函数中定义该函数。

关于AngularJS - ng-change 作为自定义指令中的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28516256/

相关文章:

javascript - 在 angularjs 中添加非作用域变量的监视

javascript - 如何通过POST方法发送json文件并在servlet上接收

angularjs - angulars $resource 是否支持 etags?

javascript - 模板未加载

javascript - 获取第一个和最后一个启用的项目

javascript - 我可以将变量 Controller 函数传递给指令中的函数链接吗?

javascript - 无法在 AngularJS ng-views 中加载 Instagram 配置文件

javascript - AngularJS 中的复杂表单验证

javascript - Angular JS 注入(inject)自定义服务

angularjs - AngularJS 中指令到指令的通信?