angular.js - angular给多个button绑定同一个事件
漂亮男人
漂亮男人 2017-05-15 17:12:48
0
4
747


想用angular 给这几个button 绑定同一个事件。
但是不想用这样的方法

漂亮男人
漂亮男人

reply all(4)
世界只因有你

Bind ng-click to p in the outer layer and pass in the $event object that comes with angular.

<p ng-click="changeMode($event);">
    <button class="btn btn-primary btn-sm">111</button>
    <button class="btn btn-primary btn-sm">222</button>
    <button class="btn btn-primary btn-sm">333</button>
    <button class="btn btn-primary btn-sm">444</button>
</p>

Controller receives $event. In $event, you can use the target object to determine which button is clicked

$scope.changeMode = function($event){
    $event.target
};

ps: I have two suggestions:
1. Boy, this is the same principle as event delegation. If you don’t have basic js skills, don’t blindly pursue any new framework, just make up for the basics.
2. Teenagers, develop good programming habits. Use button labels for buttons instead of span. Although bootstrap handles all the styling for you, this is a very poor programming habit.

淡淡烟草味

Use uib-btn-radio
http://angular-ui.github.io/b...

阿神

NG built-in mini version of jq.

If you don’t use that solution, you can write this pair of codes as a loop. In this redundant code, you can see the corresponding relationship
You can define an object literal:

$scope.types = {
  'primary':'全部时间',
  'success':'最近3天',
  'info':'最近一周',
  'warning':'最近一月',
  'danger':'最近半年'
}

<span ng-repeat="(cla,txt) in types" class="btn btn-sm btn-{{cla}}" ng-click="changeMode()">{{txt}}</span>
PHPzhong

What are your needs?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template