Reputation: 230
I a trying to update scope.answers value on change event. but not able to current changed value in link function.
directive
function textControlDir()
{
return {
transclude: true,
restrict: 'E',
require:'ngModel',
/*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,ngModel)
{
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;
}
element.bind('change',function(){
scope.answers[scope.name] = document.getElementById(scope.id).value;//element.value();
console.log(scope.answers[scope.name]);
scope.$apply();
})
}
};
}
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" data-ng-model="answers[que.QuestionData._attributeName]"></text-control-dir>
{{answers[que.QuestionData._attributeName]}}
</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>
In $scope.questions have all question, I am looping in index.html. for each question i am creating a input. if $scope.answers have value for particular input I am showing in input box. this much is working. Now I want set value in answers if user changes anything in input box.
Below code is not working of directive (element.val; gives undefined)
element.bind('change',function(){
scope.answers[scope.name] = document.getElementById(scope.id).value;//element.value();
console.log(scope.answers[scope.name]);
scope.$apply();
})
Upvotes: 0
Views: 468