I created directive for form controls[input, select and radio], Each input have default value, if $scope.answers have this input value then this value should show in input box.
Below code is not working from link fucntion
if($scope.answers.PC && $scope.answers.PC != '')
{
element.val = $scope.answers.PC;
}
Directive function
function textControlDir()
{
return {
transclude: true,
restrict: 'E',
scope: {
queObj: '=',
},
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="{{queObj._attributeName}}" class="form-control" id="{{queObj._attributeName}}" value="{{queObj._pageAttributes.defaultValue}}"></div>\n\
</div>'
,
link: function ($scope,scope, element, attrs)
{
if($scope.answers.PC && $scope.answers.PC != '')
{
element.val = $scope.answers.PC;
}
console.log($scope);
}
};
}
HTML
<text-control-dir data-que-obj="que.QuestionData"></text-control-dir>
You don't have access to the $scope
in the directives directly but as you have a link
function you can access the scope
to the respective directive. Yet you can't set values like you are doing currently .val
is a wrong way to set:
link: function(scope, element, attrs) {
if (scope.answers && scope.answers !== undefined) {
element.find('input').val(scope.answers.PC); // <---this is the way to set value
}else{
element.find('input').val('No vales found!!!');
}
}
Where element
is the wrapper element div
and you have to .find('input')
1 the text input placed inside it.
1. If jQuery
library is not included before angular, angular will use jqLite
and where .find()
method is restricted to have a lookup of tagNames only.
Just saw, your scope doesn't have answers
property. So, that won't work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.