Angularjs 如何使用元素名调用指令?
我有一个角度指示:Angularjs 如何使用元素名调用指令?,angularjs,angularjs-directive,angular-directive,Angularjs,Angularjs Directive,Angular Directive,我有一个角度指示: app.directive('myDirective', function() { return{ restrict: 'AE', scope: { myCustomer: "&" }, require: 'ngModel', link: function(scope, element, attr, ngModel){ var oldVal; function fromUser
app.directive('myDirective', function()
{
return{
restrict: 'AE',
scope: {
myCustomer: "&"
},
require: 'ngModel',
link: function(scope, element, attr, ngModel){
var oldVal;
function fromUser(value){
scope.myCustomer()(value, oldVal);
oldVal = value;
return value;
}
function toUser(value){
return value;
}
ngModel.$parsers.push(fromUser);
ngModel.$formatters.push(toUser);
}
}
}
此时,我使用属性名调用此控制器并将其绑定到函数:
<input type="text" my-directive="showInput" ng-model="user.name">
它工作得很好,但我想使用元素名,如下所示:
<my-directive>
问题是我不知道如何像绑定属性一样绑定到函数。您需要在指令定义中将“restrict”设置为“E”,例如:
bindToController: true,
controller: 'YourController',
controllerAs: 'vm',
restrict: 'E', //<----this is what you want
templateUrl: 'template.html'
bindToController:true,
控制器:“您的控制器”,
controllerAs:'vm',
restrict:'E',//您需要在指令定义中将'restrict'设置为'E',例如:
bindToController: true,
controller: 'YourController',
controllerAs: 'vm',
restrict: 'E', //<----this is what you want
templateUrl: 'template.html'
bindToController:true,
控制器:“您的控制器”,
controllerAs:'vm',
restrict:'E',//您必须在指令选项中通过restrict:'E'
angular.module("image-management")
.directive('myDirective', ($modal) => {
return {
restrict: 'E',
scope:{
showInput: '&'
},
template: '',
link: function(){}
})
<my-directive showInput="showInput" ></my-directive>
角度模块(“图像管理”)
.directive('myDirective',($modal)=>{
返回{
限制:'E',
范围:{
showInput:“&”
},
模板:“”,
链接:函数(){}
})
您必须通过指令选项中的限制:“E”
angular.module("image-management")
.directive('myDirective', ($modal) => {
return {
restrict: 'E',
scope:{
showInput: '&'
},
template: '',
link: function(){}
})
<my-directive showInput="showInput" ></my-directive>
角度模块(“图像管理”)
.directive('myDirective',($modal)=>{
返回{
限制:'E',
范围:{
showInput:“&”
},
模板:“”,
链接:函数(){}
})
然后在指令链接
功能中,可通过attr
link: function(scope, element, attr, ngModel){
// Your logic...
attr.someFunction();
}
然后在指令链接
功能中,可通过attr
link: function(scope, element, attr, ngModel){
// Your logic...
attr.someFunction();
}
正如@sumair所回答的,您可以:
<my-directive showInput="showInput" ></my-directive>
正如@sumair所回答的,您可以:
<my-directive showInput="showInput" ></my-directive>
你说的“函数”是什么意思?你应该能够将该指令用作元素我编辑了我的代码以便更好地理解。我使用解析器和格式化程序来监视数据更改…当数据更新时,会执行一个函数,在本例中为showInput函数。我想编写my directive=“showInput”使用DOM Element“函数”是什么意思?您应该能够使用指令作为元素我编辑了我的代码以便更好地理解。我使用解析器和格式化程序来监视数据更改…当数据更新时,执行函数,在本例中为showInput函数。我想编写my directive=“showInput”使用DOM Element以及如何在html中使用元素而不是属性调用此指令。现在我有了我的指令=“show input”但是我想用这样的函数我已经更新了我的答案请检查如何在htmlAnd中调用这个指令如何在html中使用元素而不是属性调用这个指令。现在我有了我的指令=“show input”但是我想用这样的函数我已经更新了我的答案请检查如何在htmlAnd中调用这个指令如何在html中使用元素而不是属性调用这个指令。现在我有了我的指令=“show input”但是我想在函数中使用类似的东西,以及如何在html中使用元素而不是属性调用该指令