我在循环中动态创建指令。其中每个输入名称动态创建。
在同一个我想根据问题更新范围值。 $ scope对象名称应该是动态的。比如$ scope.answers.xxx
其中xxx是问题名称。
$ scope.question都有问题
并希望在$ scope.answers中维护答案。
指令功能
function textControlDir()
{
return {
transclude: true,
restrict: 'E',
/*scope: {
queObj: '=',
selectedAns: '='
},*/
template: '<div class="form-group">\n\
<label for="{{queObj._attributeName}}" class="col-sm-5 control-label">{{queObj._text}}</label>\n\
<div class="col-sm-6"><input type="text" name="{{name}}" class="form-control" id="{{id}}" value="{{selectedAns}}"></div>\n\
</div>',
link: function (scope, element, attrs)
{
var queObj = scope.que.QuestionData;
scope.queObj = scope.que.QuestionData;
scope.name = queObj._attributeName;
scope.id = queObj._attributeName;
var selectedAns = '';
if(scope.answers)
{
selectedAns = scope.answers[scope.name];
}
if(selectedAns && selectedAns != '')
{
scope.selectedAns = selectedAns;
}
else
{
scope.selectedAns = scope.queObj._pageAttributes.defaultValue;
}
}
};
}
HTML
<div ng-repeat="que in questions[$state.current.name]">
<div ng-if="que.QuestionData._fieldType === 'text'" >
<text-control-dir data-que-obj="que.QuestionData" ></text-control-dir>
</div>
<div ng-if="que.QuestionData._fieldType === 'select'" >
<select-control-dir data="que.QuestionData"></select-control-dir>
</div>
<div ng-if="que.QuestionData._fieldType === 'radio'" >
<radio-control-dir data="que.QuestionData"></radio-control-dir>
</div>
<div ng-if="que.QuestionData._fieldType === 'hidden' && que.QuestionData._attributeName != 'CBQ'" >
<hidden-control-dir data="que.QuestionData"></hidden-control-dir>
</div>
</div>
为完整代码创建了plunker http://plnkr.co/edit/Op1QDwUBECAosPUC7r3N?p=preview
答案 0 :(得分:1)
如果没有scope.$apply
或ngModelController
<div ng-repeat="que in questions[$state.current.name]">
<div ng-if="que.QuestionData._fieldType === 'text'" >
<input type="text" data-ng-model="que.QuestionData.Answer">
</div>
</div>
var app = angular.module("app",[]);
var ctrlFn = function(){
$scope.questions = questions;\\assign scope here
};
var ctrl = app.contoller('ctrl',ctrlFn);
ctrlFn.$inject = ['$scope'];
答案 1 :(得分:0)
使用:
$scope.answers[questionName] = data;