0

Given the following:

<div ng-app="interactive">  
    <main ng-controller="RecipesController">
        <ul>
            <li ng-repeat="option in options">
                <ul decisionIndex="{{ value }}">
                    <li>Edit {{ option.name }}</li>
                    <li>Choose {{ option.name }}</li>
                    <li>Delete {{ option.name }}</li>
                </ul>
            </li>
        </ul>
    </main>
</div>

How can I use $watch to detect whether "value" (is either 0, 1 or 2) changes?

Do I need to use $watchGroup ?  As far as I know that only works if you have multiple attributes which are not the same.

I found this working with directives and isolated scopes, but it does not quite match my case and I would prefer   not   using directives.
Is it possible using isolated scopes without directives? Or how can I watch for changes of an attribute-value occuring multiple times?

EDIT:
This is how I tried using $watch

app.controller('RecipesController', ['$scope', function ($scope) {
    $scope.$watch('value', function () {
        console.log('value: ' + $scope.value);
    });
}]);
Community
  • 1
  • 1
Slotty Bon
  • 101
  • 6

1 Answers1

0
<div ng-app="myApp" ng-controller="myCtrl">
    <input type="text" ng-model="value">
    <p>Current value is: {{value}}</p>
    <ul>
        <li ng-repeat="option in options">
            <ul decisionIndex="{{value}}">
                <li ng-click="setValue(0)">Edit {{option.name}}</li>
                <li ng-click="setValue(1)">Choose {{option.name}}</li>
                <li ng-click="setValue(2)">Delete {{option.name}}</li>
            </ul>
        </li>
    </ul>
</div>

Controller:

angular.module('myApp', [])
  .controller('myCtrl', ['$scope', function($scope) {
    $scope.value = 0; //Set initial value, or just declare it;
    $scope.options = [
        {name: "Bob"},
    ];
    $scope.setValue = function(a){
        $scope.value = a;
    }
    $scope.$watch('value', function(newVal, oldVal){
        console.log(newVal);
    })
  }]);
ZenDD
  • 906
  • 1
  • 7
  • 16