Answer the question
In order to leave comments, you need to log in
How to fix render error in angularjs when using directives?
I recently started learning angularjs and there was a situation with rendering.
html
<radiogroup>
<radio checked>Option 1</radio>
<radio>Option 2</radio>
</radiogroup>
var application = angular.module('application', []);
application.directive('radiogroup', function(){
return {
restrict: 'E',
transclude: true,
scope: {},
controller: function($scope, $element) {
var radios = $scope.radios = [];
this.check = function(radio) {
angular.forEach(radios, function(radio) {
radio.checked = false;
});
radio.checked = true;
}
this.addToGroup = function(radio, checked) {
if (checked) this.check(radio);
radios.push(radio);
};
},
template: '<div class="radiogroup" ng-transclude></div>',
replace: true
}
})
.directive('radio', function() {
return {
require: '^radiogroup',
restrict: 'E',
transclude: true,
scope: {},
link: function(scope, element, attrs, radiogroupCtrl) {
radiogroupCtrl.addToGroup(scope, angular.isUndefined(attrs.checked) ? false : true);
element.bind('click', function(){
radiogroupCtrl.check(scope);
});
},
template: '<div class="radio" ng-class="{active: checked}" ng-transclude></div>',
replace: true
}
});
Answer the question
In order to leave comments, you need to log in
Because the $digest loop doesn't run. Read something about how data-binding works in angularjs.
In short, for each asynchronous change to $scope, you need to run $digest or $apply. The difference is this: $digest starts updating only the current and child scopes. $apply updates all all all. therefore in such pieces it is better to use $apply.
element.bind('click', function(){
scope.$apply(function () {
radiogroupCtrl.check(scope);
});
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question