首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AngularJS -ng-在自定义指令中作为选项进行更改

AngularJS -ng-在自定义指令中作为选项进行更改
EN

Stack Overflow用户
提问于 2015-02-14 13:47:29
回答 1查看 3K关注 0票数 2

我想在自定义指令中添加一个ng-change函数,但我不确定ng-change是否会进入模板或链接函数,另外,我还想使ng-change成为可选的(调用‘

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

代码语言:javascript
复制
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;
            };
        }
    };
}

如果能帮忙就好了谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-14 17:07:15

您应该能够使用类似于这把小提琴的格式。

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

示例:

代码语言:javascript
复制
<test-change value="blah" check-exceptions="true" ng-model="foo"/>

这应该是最直截了当的方法。

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28516256

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档