1
<ul>
    <li  ng-repeat="val in [1,2,3]">
    <button ng-click="dummy = '{{val}}'">{{val}}</button>
    </li>
</ul>
<div>Value : {{dummy}}</div>

I am trying to assign a value when the button is clicked but its not working.

Luthando Ntsekwa
  • 4,192
  • 6
  • 23
  • 52
Kaagesh
  • 121
  • 1
  • 1
  • 5

3 Answers3

4

ng-repeat creates its own scope. Since the button is in the ng-repeat, it has an individual scope, and can't access the dummy value. To prevent this, your need to use a dot in your ng-model.

Here is your code, corrected: http://jsfiddle.net/844k52bh/

Community
  • 1
  • 1
Deurco
  • 530
  • 2
  • 8
1

http://jsfiddle.net/sg39rypn/

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {

    $scope.changeDummyVal=function(val){
        $scope.dummy=val;
    }
});

You can make function for it

Man Programmer
  • 5,300
  • 2
  • 21
  • 21
0

You just need to do:

ng-click="dummy = val "

i.e

<ul>
<li  ng-repeat="val in [1,2,3]">
<button ng-click="dummy = val ">{{val}}</button>
</li>
</ul>
<div>Value : {{dummy}}</div>
Hmahwish
  • 2,222
  • 8
  • 26
  • 45