【发布时间】:2016-12-27 05:08:48
【问题描述】:
我有这个表格部分(为问题而简化):
<label>firstitem brand: </label><input ng-model="ctrl.object.item_attributes[0].brand">
<label>firstitem model: </label><input ng-model="ctrl.object.item_attributes[0].name">
<label>seconditem brand: </label><input ng-model="ctrl.object.item_attributes[1].brand">
<label>seconditem model: </label><input ng-model="ctrl.object.item_attributes[1].name">
我真的很想做这样的事情:
<form-directive item="ctrl.object.item_attributes[0]"></form-directive>
<form-directive item="ctrl.object.item_attributes[1]"></form-directive>
作为一个 Angular 新手,我对如何通过我的指令设置 ng-model 感到困惑。我试过直接通过范围传递它,并通过指令的链接函数,但没有运气。有什么想法吗?'
编辑:
我根据 Steff 的建议尝试了以下指令:
function fixIt () {
return {
restrict: 'EA',
require: "ngModel",
link: function (scope, elem, attrs, ngModel) {
attrs.ngModel = scope.comp
// scope.com = object.item_attributes[i]
}
}
};
angular
.module('app')
.directive('fixIt', fixIt);
我的输入格式如下:
<input fix-it ng-model="">
我收到以下错误:angular.self-7f8df3e....js?body=1:13921 错误:[ngModel:nonassign] Expression '' is non-assignable。
如果我在我的输入中给 ng-model 一个初始值,那么错误就会消失,但我在 fixIt 指令内的链接函数中没有做任何事情。
【问题讨论】:
标签: angularjs angularjs-directive angular-ngmodel