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,056
  • 5
  • 22
  • 49
Kaagesh
  • 101
  • 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
  • 531
  • 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,150
  • 2
  • 17
  • 18
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,202
  • 7
  • 24
  • 44