在 ng-click angular.js 中传递按钮 ID [英] pass button id in ng-click angular.js
问题描述
我需要在 Ionic 框架中传递按钮 ID.
这是我尝试过的.
在js代码中:
angular.module('todo', ['ionic']).controller('TodoCtrl', function($scope) {{$scope.showAlert = 函数(btnId){警报(btnId);};}});
在 html 中:
O/P:未定义
或
O/P:未定义
或
O/P:未定义
或
O/P:未定义
或
O/P:btnId
在函数中直接写按钮id的方法正确吗?
我参考了一些答案,例如 this.所以我认为我在使用它时犯了一些错误.请让我知道我需要更改什么.
哟,检查这个要点:
https://gist.github.com/lc-nyovchev/ed0a640a82a0f2dfd5a1
这是一种非常简单和天真的方法.
<button data-ng-attr-id="btnId" class="button button-light" data-ng-click="showAlert(btnId)">点击我按钮>
或者你可以在你的控制器中:
$scope.btnId = 'asd';
那么你就不需要 ng-init 块 div.
或者您可以在 ng-click 中获取 $event 的句柄,并获取其目标,然后获取其 id,但我不建议这样做,这不是有角度的做事方式:>
I need to pass button id in Ionic Framework.
Here is what I have tried.
In js code:
angular.module('todo', ['ionic'])
.controller('TodoCtrl', function($scope) {
{
$scope.showAlert = function(btnId) {
alert(btnId);
};
}
});
In html:
<button id="a" class="button button-light" data="{{button.id}}" ng-click="showAlert(data.id)">
Click Me
</button>
O/P: undefined
or
<button id="a" class="button button-light" data="{{button.id}}" ng-click="showAlert(data)">
Click Me
</button>
O/P: undefined
or
<button id="a" class="button button-light" data="{{event.id}}" ng-click="showAlert(data.id)">
Click Me
</button>
O/P: undefined
or
<button id="a" class="button button-light" ng-click="showAlert(this.id)">
Click Me
</button>
O/P: undefined
or
<button id="btnId" class="button button-light" ng-click="showAlert('btnId')">
Click Me
</button>
O/P: btnId
Is this correct way to directly write id of button in function?
I referred to a few answers like this. So I think I am making some mistake in using it. Please let me know what I need to change.
Yo, check this gist:
https://gist.github.com/lc-nyovchev/ed0a640a82a0f2dfd5a1
That is a very easy and naive way to do it.
<div data-ng-init="btnId='asd';">
<button data-ng-attr-id="btnId" class="button button-light" data-ng-click="showAlert(btnId)">
Click Me
</button>
</div>
Or you can have in your controller:
$scope.btnId = 'asd';
Then you don't need the ng-init block div.
Or you can get a handle to the $event in your ng-click, and get its target, and then get its id, but I wouldn't recommend that, it is not the angular way of doing things:
<button id="bla" class="button button-light" data-ng-click="showAlert($event)">
Click Me
</button>
$scope.showAlert = function(event){
alert(event.target.id);
}
这篇关于在 ng-click angular.js 中传递按钮 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!