Hi there,
I'm a Angular newbie and here is what I would like to do.
Main directive 'question' with an inner 'answer'.
It looks as follow:
<script type="text/ng-template" id="question.html">
<div class="col-md-12">
<label>{{question.text}}</label>
</div>
<div class="col-md-12">
<div answer class="checkbox">
<span ng-if="isSelected()" class="glyphicon
glyphicon-check"></span>
<span ng-if="!isSelected()" class="glyphicon
glyphicon-unchecked"></span>
<a>{{answer.text}}</a>
</div>
</div>
</script>
Question works pretty ok, but I have a problem with an answer directive.
Essentially, I would like to multiply answer with a data that is stored in
question.values array.
I use angular.forEach which creates new element with an isolated scope.
Works great.
Th problem is I want to assign onClick() to each isolated scope.
Here's the code:
energyApp.directive('answer', function ($compile, OfferReq) {
var linker = function ($scope, element, attrs) {
var questionId = $scope.$parent.question.id;
element.removeAttr('answer');
angular.forEach($scope.$parent.question.values, function (value) {
$scope = $scope.$parent.$new(true);
$scope.questionId = questionId;
// I assign answer to the isolated scope
$scope.answer = value;
var newEl = element.clone();
newEl.attr('ng-click', 'onClick(answer)');
element.after(newEl);
$compile(newEl.contents())($scope);
});
}
var controller = function ($scope) {
var questionId = $scope.$parent.question.id;
$scope.onClick = function (answer) {
console.log("Click: " + answer);
// prints: "Click: undefined"
}
}
return {
restrict: "EA",
link: linker,
controller: controller,
replace: true,
scope: false
}
});
It looks like controller doesn't see answer in its scope.
What kind of scope is passed to the controller?
Thanks for any ideas on how to solve the problem!
Regards,
Piotr
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.