W
W
WebDev2015-04-24 16:16:02
Angular
WebDev, 2015-04-24 16:16:02

How to set value on ng-click?

Hello, tell me please, there is a controller, in it
$scope.template = '';
In the view I add to the element

<button ng-click="template = 2">Set template</button>

So assigning a value does not work, although it seems that they write what is possible. You have to write like this:
<button ng-click="setSelectedTemplate(2)">Set template</button>

And describe a method in a controller that already assigns a value.
It works, but you have to write a setter. Is it possible to assign directly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gavrilov, 2015-04-24
@FireVolkhov

Most likely your button is in ng-ifor in another directive that creates $scopebecause of this the value is assigned somewhere there.
Alternatively, you can use it in the controller

$scope.customObj = 
    template: ''

and in templateng-click = "customObj.template = 2"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question