如何传递ng模型对象而不是指令内部的值
How pass a ng-model object instead of a value inside of a directive?
我制作了一个输入指令,以在多个表单中使用它,所以我需要在提交表单时传递一个ng模型对象,而不是指令中的值。
这是代码
HTML:
<form name="formo" novalidate ng-submit="saveOrganization(org)">
....
<responsible-select model="org.collaborators" show="1" id="id" adding="0" ></responsible-select>
....
</form>
指令:
angular.module('app').
directive('responsibleSelect', function() {
return {
restrict: 'E',
scope: {
model :"=",
show : '=',
id : '=',
adding : '='
},
controller: 'findCollabOrResCtrl',
template: '<ui-select ng-model="model" reset-search-input="true" tagging="tagTransform" multiple theme="bootstrap">'+
'<ui-select-match placeholder="Escribe los emails de los colaboradores del área" >'+
'{{$item.email}}'+
'</ui-select-match>'+
'<ui-select-choices refresh="refreshCollaborator($select.search,{{ show }},{{ id }},{{ adding }} )"refresh-delay="0" repeat="collaborator.email as collaborator in collaborators">'+
'{{collaborator.email}} '+
'</ui-select-choices>'+
'</ui-select>'
};
});
输出html:
<responsible-select model="org.collaborators" show="1" id="1" adding="0" class="ng-isolate-scope">
<div ng-model="model" >...</div>
</responsible-select>
我需要这个输出:
<responsible-select model="org.collaborators" show="1" id="1" adding="0" class="ng-isolate-scope">
<div ng-model="org.collaborators" >...</div>
</responsible-select>
这可能吗?
对指令使用"require"。
angular.module('app').
directive('responsibleSelect', function() {
return {
restrict: 'E',
require: 'ngModel'
scope: {
model :"=",
show : '=',
id : '=',
adding : '='
},
controller: 'findCollabOrResCtrl',
template: '<ui-select ng-model="model" reset-search-input="true" tagging="tagTransform" multiple theme="bootstrap">'+
'<ui-select-match placeholder="Escribe los emails de los colaboradores del área" >'+
'{{$item.email}}'+
'</ui-select-match>'+
'<ui-select-choices refresh="refreshCollaborator($select.search,{{ show }},{{ id }},{{ adding }} )"refresh-delay="0" repeat="collaborator.email as collaborator in collaborators">'+
'{{collaborator.email}} '+
'</ui-select-choices>'+
'</ui-select>'
};
});
此外,在控制器中定义paramethers。第四个参数将是ngModelCtrl,它是ngModel指令的控制器,您可以在其中获取值。
您需要定义"链接"函数来接受ngModel的控制器。
link: function ($scope, $element, attrs, ngModelCtrl)
并在链接函数中获取值并传递给指令的控制器。
如果ngModel的要求是可选的,例如您可以不使用它-使用"?ngModel’。
相关文章:
- 角度控制器功能不;在指令内部调用时,似乎无法始终如一地工作
- 如何在angularjs中对指令内部的JSON数组使用过滤器
- $watch使用隔离范围时从角度指令内部更改值
- Angular 指令内部 HTML 重新编译,动态创建绑定属性
- 从指令内部访问内插值
- AngularJS:如何在指令编译和链接后从指令内部运行JavaScript
- 如何传递ng模型对象而不是指令内部的值
- 指令内部的DOM查询
- 将html附加到具有范围值的指令内部
- Angular ui路由器阻止指令内部的状态触发
- http调用位于指令内部的控制器内部
- 从另一个控制器中的指令内部定义的一个控制器调用方法:AngularJS
- Angular从指令内部的输入中删除无效内容
- angular-如果src错误,则在指令内部拼接数组
- 如何在指令内部用innerHTML插入的html上使用ng-bind
- 改变ng-repeat from指令内部的作用域
- AngularJS:在指令内部的链接函数中访问json响应
- 如何从指令内部观察变量的变化
- 指令内部的模板
- 在没有应用指令函数的指令内部重复