質問

I'm beginner with angular, and I don't know how to get an attribute 's value. As exemple, I have a button with a value attribute, and I'would like to get that value to play with, when I call a function, in this case the function changeArea, here is the code.

In this case I want to get the value of the value attribute which is 0. But I have no idea on the way I can access it.

<button class="btn btn-{{ (gesture.area_id == '0') && 'primary' || 'default' }}" 
    type="button" 
    value="0" 
    ng-click="changeArea(gesture.area_id,gesture.id)">
</button>

EDIT:

Let'say I have several buttons and I need them to have a unique value. Can I do something like that to the others value attribute: {{myButtonValue+1}} and then for the other {{myButtonValue+2}} and so on ? Sorry, I can't test it at the moment, that's why I ask.

役に立ちましたか?

解決

When you do ng-click you can pass the click event $event to your function.

<button class="btn btn-{{ (gesture.area_id == '0') && 'primary' || 'default' }}" 
    type="button" 
    value="0" 
    ng-click="changeArea(gesture.area_id,gesture.id,$event)">
</button>

Then in your $scope.changeArea you can do this:

$scope.changeArea = function (param1, param2, e)

From there you should be able to grab the elements values.

https://docs.angularjs.org/guide/expression#-event-

他のヒント

Set it to a $scope variable and use that:

<button value="{{myButtonValue}}"

And then you can do:

$scope.myButtonValue = 3; //or whatever
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top