AngularJS指令位于ng重复循环内,带有基于变量的templateUrl
好吧,我对AngularJS这件事有点陌生,我的手可能比我应该做的更脏了,但我想做的是:AngularJS指令位于ng重复循环内,带有基于变量的templateUrl,angularjs,angularjs-directive,angularjs-ng-repeat,Angularjs,Angularjs Directive,Angularjs Ng Repeat,好吧,我对AngularJS这件事有点陌生,我的手可能比我应该做的更脏了,但我想做的是: 循环浏览客户实体的字段数组(实际上是对象数组) 调用从字段属性中选择模板的指令 数据将实际客户字段绑定到模板内的ng模型 显示字段 以下是我目前掌握的情况: 循环HTML <div ng-repeat="info in customerCtrl.personalInfoFields"> <edit-field info="info" model="customerCtrl.cu
- 循环浏览客户实体的字段数组(实际上是对象数组)
- 调用从字段属性中选择模板的指令
- 数据将实际客户字段绑定到模板内的ng模型
- 显示字段
<div ng-repeat="info in customerCtrl.personalInfoFields">
<edit-field info="info" model="customerCtrl.customer"></edit-field>
</div>
指令:
myApp.directive('editField', ['$http', '$compile', 'capitalizeFilter', function($http, $compile, $capitalizeFilter) {
return {
scope: {
info: "=",
model: "="
},
replace: true,
template: '<div ng-include="url"></div>',
link: function(scope, element, attrs){
scope.url = '/views/fields/edit'+$capitalizeFilter(scope.info.type)+'.html';
}
/*templateUrl: function(elem,attrs)
{
if(typeof attrs.info.type === "undefined")
return '/views/fields/editText.html';
return '/views/fields/edit'+attrs.info.type+'.html'
},*/
};
}]);
myApp.directive('editField',['$http','$compile','capitalfilter',函数($http,$compile,$capitalfilter){
返回{
范围:{
信息:“=”,
型号:“=”
},
替换:正确,
模板:“”,
链接:函数(范围、元素、属性){
scope.url='/views/fields/edit'+$capitalifilter(scope.info.type)+'.html';
}
/*templateUrl:函数(元素、属性)
{
if(attrs.info.type的类型==“未定义”)
返回“/views/fields/editText.html”;
返回'/views/fields/edit'+attrs.info.type+'.html'
},*/
};
}]);
editText.html(其他文件现在没有太大区别,稍后再做)
{{'customers.'+info.field | i18n |大写}
现在,在字段中,我刚刚得到[object object],如果我将实际字段本身作为模型传递,而不是整个customer对象,它会显示良好,但没有绑定(我可以更改字段内容,但它不会反映在controller对象中)
注释的templateUrl部分不起作用,因为AngularJS只为整个ng repeat函数编译URL一次,所以我只能为每个人使用未定义的变量->editText
如何在为每种字段类型获取正确模板的同时成功绑定字段?问题已解决,显然正在调用
$compile(element.contents())(scope);
修复了绑定,但我仍然必须将整个customer对象传递给指令,然后使用
ng-model="model[info.field]"
达到预期的效果
$compile(element.contents())(scope);
ng-model="model[info.field]"