angularjs - pass button id in ng-click angular.js -
i need pass button id in ionic framework.
here 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 correct way directly write id of button in function?
i referred few answers this. think making mistake in using it. please let me know need change.
yo, check gist:
https://gist.github.com/lc-nyovchev/ed0a640a82a0f2dfd5a1
that easy , naive way 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 can have in controller:
$scope.btnid = 'asd';
then don't need ng-init block div.
or can handle $event in ng-click, , target, , id, wouldn't recommend that, not 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); }
Comments
Post a Comment