I have the following situation:
In my app I have a quiz, I store the score and the current question as $scope.current, $scope.score in MainCtrl, then I have a directive called question where I show the question and the choices like:
angular.module('quiz')
.directive('myQuestion', function() {
return {
restrict: 'EA',
replace: true,
scope: {
question: '=',
index: '='
},
link: function(scope) {
scope.validate = function(index) {
var isCorrect = index === scope.question.correct;
if(isCorrect) {
console.log('Correct!');
//$scope.score += 10 ($scope score from MainCtrl)
}
if(!isCorrect) {
console.log('Incorrect!');
scope.correct = false;
}
//$scope.current += 1;
};
templateUrl: 'assets/javascript/tpl/question.html'
};
});
In my html I have the following structure:
<div class="quiz">
<h2>Current Question: {{current}}</h2>
<h2>Your score: {{score}}</h2>
<my-question ng-repeat="q in questions track by $index"
ng-if="isCurrent($index)" //in controller i compare $index with $scope.current
question="q"
index="$index"></my-question>
</div>
My question is, how can I update $scope.score or $scope.current from the directive link function?
Can someone explain me please the best way?