AngularJS - NG-变化作为自定义指令的选项 [英] AngularJS - ng-change as an option in a custom directive
问题描述
我要添加 NG-变化
功能到自定义指令,但我不知道,如果 NG-变化
会在模板中或进入链接功能,另外我想使 NG-变化
可选的(叫
I want to add an ng-change
function into a custom directive but I'm not sure if ng-change
will go inside the template or into link function, additionally I'd like to make the ng-change
optional (calling '
<文本编辑就地值=loan.due_date变化=checkException
参数=foobar的>
<text-edit-in-place value="loan.due_date" change="checkException" param="foobar">
在这里留下了变化=放'将添加 checkException('foobar的')
功能;参数=不会运行任何 NG-变化
)。
' would add the checkException('foobar')
function where leaving out change= & param= would not run any 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;
};
}
};
}
帮助将AP preciated,谢谢你。
Help would appreciated, Thanks.
推荐答案
您应该能够使用类似的这拨弄
You should be able to use a format similar to This fiddle
在该格式,您可以在指令中定义的函数,然后指定你是否希望被调用的函数。
In that format, you can define the function within the directive, then specify whether or not you want the function to be called.
例如:
<test-change value="blah" check-exceptions="true" ng-model="foo"/>
这应该是最直接的方式来做到这一点。
That should be the most straight-forward way to do it.
然后就可以在指令中的链接功能中定义的功能。
Then you can define the function within the link function in the directive.
这篇关于AngularJS - NG-变化作为自定义指令的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!