angularjs 如何在一次点击中添加多个功能?

swvgeqrz  于 2022-11-28  发布在  Angular
关注(0)|答案(9)|浏览(202)

我一直在寻找如何执行这个,但我不能找到任何相关的到目前为止,:(我可以嵌套两个函数是的,但我只是想知道这是可能的吗?我想这样做字面上:

<td><button class="btn" ng-click="edit($index) open()">Open me!</button></td>

目前我的JS代码:

$scope.open = function () {
  $scope.shouldBeOpen = true;
};      

$scope.edit = function(index){

  var content_1, content_2;
      content_1 = $scope.people[index].name;
      content_2 = $scope.people[index].age;

  console.log(content_1);
};

我想一次点击就能调用两个函数,在angularJS中怎么做呢?我以为在CSS中添加多个类时会很简单...但事实并非如此:(

nlejzf6q

nlejzf6q1#

您有两个选项:
1.创建第三个方法来 Package 这两个方法。这样做的好处是你在模板中放置了更少的逻辑。
1.否则,如果您想在ng-click中添加2个调用,则可以添加';“edit($index)之后就这样
ng-click="edit($index); open()"
请参阅此处:http://jsfiddle.net/laguiz/ehTy6/

6yoyoihd

6yoyoihd2#

您可以使用';'

ng-click="edit($index); open()"
ohfgkhjo

ohfgkhjo3#

很多人使用(点击)选项,所以我也会分享这个。

<button (click)="function1()" (click)="function2()">Button</button>
txu3uszq

txu3uszq4#

添加多个函数的标准方法

<button (click)="removeAt(element.bookId); openDeleteDialog()"> Click Here</button>

<button (click)="removeAt(element.bookId)" (click)="openDeleteDialog()"> Click Here</button>
lskq00tm

lskq00tm5#

试试这个:

  • 创建函数集合
  • 创建一个循环访问并执行集合中所有函数的函数。
  • 将函数添加到html
array = [
    function() {},
    function() {},
    function() {}
]

function loop() {
    array.forEach(item) {
        item()
    }
}

ng - click = "loop()"
euoag5mw

euoag5mw6#

遵循以下

ng-click="anyFunction()"

anyFunction() {
   // call another function here
   anotherFunction();
}
8fq7wneg

8fq7wneg7#

<!-- Button trigger modal -->
                <button type="button" (click)="open(content)" style="position: fixed;  bottom: 0;  right: 130px;"
                    class="btn col-sm-1  btn-Danger" >
                    Reject
                </button>
                  
                  

                <ng-template #content let-modal>
                    <div class="modal-header">
                      <h4 class="modal-title" id="modal-basic-title">Profile update</h4>
                      <button type="button" class="btn-close" aria-label="Close" (click)="modal.dismiss('Cross click')"></button>
                    </div>
                    <div class="modal-body">
                     
                        <div class="mb-3">
                          <label class="bg-danger text-light" for="Reject">Reason For reject</label>
                          
                            <textarea   matInput placeholder="  Reject" [(ngModel)]="asset_note">{{note}}</textarea>
                
                        </div>
                    </div>
                    <div class="modal-footer">
                        <!-- -->
                      <button type="button" class="btn btn-outline-dark" (click)="reject();modal.close('Save click') ">Save</button>
                    </div>
                  </ng-template> 

**.ts file**
open(content: any) {
    this.modalService.open(content, {ariaLabelledBy: 'modal-basic-title'}).result.then((result) => {
      this.closeResult = `Closed with: ${result}`;
    }, (reason) => {
      this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
    });
  }

  private getDismissReason(reason: any): string {
    if (reason === ModalDismissReasons.ESC) {
      return 'by pressing ESC';
    } else if (reason === ModalDismissReasons.BACKDROP_CLICK) {
      return 'by clicking on a backdrop';
    } else {
      return `with: ${reason}`;
    }
  }

close()
{

this.getDismissReason(ModalDismissReasons.ESC);
}
kzmpq1sx

kzmpq1sx8#

以下哪一项是最佳做法(选项1或选项2)
1.=“删除在(元素.bookId); openDeleteDialog()"〉单击此处
1.单击此处,然后单击“删除”按钮。

bvn4nwqk

bvn4nwqk9#

ng-click "$watch(edit($index), open())"

相关问题