Thy
Thy

Reputation: 31

AngularJs: ng-click not working for certain case

There is clearly something fundamental but I can't search any other useful posts on this.

For first case, it worked as expected. When click on button B, I am getting 2. For second case, it is not working anymore when I changed the numbers to A&B. Please find the code as below.

First Case

<p>Click the button:</p>

<button ng-click="name = 1" ng_init="name=0">A</button>
<button ng-click="name = 2" ng_init="name=0">B</button>

<p>This is {{name}}.</p>

Second Case

<p>Click the button:</p>

<button ng-click="name = A" ng_init="name=0">A</button>
<button ng-click="name = B" ng_init="name=0">B</button>

<p>This is {{name}}.</p>

Upvotes: 2

Views: 39

Answers (2)

Gustavo Gabriel
Gustavo Gabriel

Reputation: 1296

A and B are probably strings, so you should assign it as a string.

<button ng-click="name = 'A' " ng_init="name=0">A</button>
<button ng-click="name = 'B' " ng_init="name=0">B</button>

Hope it helps :)

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691635

name = A

assigns the value of $scope.A to $scope.name. I assume you don't have any property named A in the scope, so name is set to undefined. You probably want

name = 'A'

Upvotes: 3

Related Questions