AngularJS, ng-repeat on radio and $scope.watch -


i'm using example: https://jsfiddle.net/qnw8ogrk/1/ create radio-buttons.

i able use $scope.watch on radio-buttons, i'm not able use:

$scope.watch('selected', ... 

what should assign .watch able detect whenever user clicks on radio button?

actual mistake $scope.watch should $scope.$watch having watcher on scope variable not idea. instead of placing watcher on ng-model i'd suggest use ng-change fires when radio button ng-model value gets changed.

<label ng-repeat="option in options">    <input type="radio" ng-change="test()" ng-model="$parent.selected" ng-value="option" />{{option}} </label> 

and don't use rid of $parent notation accessing outer scope of ng-repeat switch use controlleras pattern, , change ng-controller directive use alias of controller. change controller implementation, bind value this(context) instead of $scope

markup

<div ng-controller="maincontroller vm">     <label ng-repeat="option in vm.options">       <input type="radio" ng-change="vm.test()" ng-model="vm.selected" ng-value="option" />{{option}}     </label> </div> 

controller

appmodule.controller('maincontroller', function($scope) {   var vm = this;   vm.selected = 'red';   vm.options = ['red', 'blue', 'yellow', 'green'];   vm.test = function() {     alert('changed');   } }); 

forked plunkr


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -