在ng-click angular.js中传递按钮ID [英] pass button id in ng-click angular.js

查看:553
本文介绍了在ng-click angular.js中传递按钮ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Ionic Framework中传递按钮ID。

I need to pass button id in Ionic Framework.

这是我尝试过的。

在js代码中:

angular.module('todo', ['ionic'])
.controller('TodoCtrl', function($scope) {
    {
        $scope.showAlert = function(btnId) {
            alert(btnId);
        };
    }
});

在html中:

<button id="a" class="button button-light"  data="{{button.id}}" ng-click="showAlert(data.id)">
    Click Me
</button>

O / P: undefined

<button id="a" class="button button-light"  data="{{button.id}}" ng-click="showAlert(data)">
    Click Me
</button>

O / P: undefined

<button id="a" class="button button-light"  data="{{event.id}}" ng-click="showAlert(data.id)">
    Click Me
</button>

O / P: undefined

<button id="a" class="button button-light" ng-click="showAlert(this.id)">
    Click Me
</button>

O / P: undefined

<button id="btnId" class="button button-light" ng-click="showAlert('btnId')">
    Click Me
</button>

O / P: btnId

这是在函数中直接写入按钮id的正确方法吗?

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.

推荐答案

哟,请查看这个要点:

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'; 

然后你不需要ng-init块div。

Then you don't need the ng-init block div.

或者你可以在ng-click中获得$ event的句柄,并获得它的目标,然后得到它的id,但我不建议这样做,这不是有角度的做法东西:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆