angular笔记 directive scope 调用父scope带参数方法

在使用angular directive的时候,为了不污染环境,我们为给directive自己一个scope,如果这个scope需要使用父类中scope的对象,会使用 = & @三种标示,在开发项目中,我遇到需要调用父类带参数方法的情况,自然而然我想到了使用 &标示符,可是如何进行带参数传递呢?

成功代码:

script:

angular.module('docsIsoFnBindExample', [])
.controller('Controller', ['$scope', '$timeout', function($scope, $timeout) {
  $scope.name = 'Tobias';
  $scope.message = '';
  $scope.hideDialog = function (message) {
    $scope.message = message;
    $scope.dialogIsHidden = true;
    $timeout(function () {
      $scope.message = '';
      $scope.dialogIsHidden = false;
    }, 2000);
  };
}])
.directive('myDialog', function() {
  return {
    restrict: 'E',
    transclude: true,
    scope: {
      'close': '&onClose'
    },
    templateUrl: 'my-dialog-close.html'
  };
});

index.html

<div ng-controller="Controller">
  {{message}}
  <my-dialog ng-hide="dialogIsHidden" on-close="hideDialog(message)">
    Check out the contents, {{name}}!
  </my-dialog>
</div>


my-dialog-close.html

<div class="alert">
  <a href class="close" ng-click="close({message: 'closing for now'})">×</a>
  <div ng-transclude></div>
</div>


我们发现 在my-dialog-colse.html 中,使用了 ng-click="close({message:'closing for now'})",这种传递参数方式让我想起了jsonp方法,这样 在父scope ,hideDialog方法就会拥有message值。

同理 如果我们要使用两个参数,对原有代码进行改变

script:

angular.module('docsIsoFnBindExample', [])
.controller('Controller', ['$scope', '$timeout', function($scope, $timeout) {
  $scope.name = 'Tobias';
  $scope.message = '';
  $scope.hideDialog = function (message,status) {               //多带了一个参数 status
    $scope.message = message;
    $scope.dialogIsHidden = true;
    $timeout(function () {
      $scope.message = '';
      $scope.dialogIsHidden = false;
    }, 2000);
  };
}])
.directive('myDialog', function() {
  return {
    restrict: 'E',
    transclude: true,
    scope: {
      'close': '&onClose'
    },
    templateUrl: 'my-dialog-close.html'
  };
});



index.html

<div ng-controller="Controller">
  {{message}}{{status}}
  <my-dialog ng-hide="dialogIsHidden" on-close="hideDialog(message,status)">     
    Check out the contents, {{name}}!
  </my-dialog>
</div>


my-dialog-close.html

<div class="alert">
  <a href class="close" ng-click="close({message: 'closing for now',status:'1'})">×</a>           //增加status参数
  <div ng-transclude></div>
</div>

注意 :
在index.html 中ng-close="hideDialog( message,status)"  这俩参数名(message,status) 必须和 my-dialog-close.html中 ng-click="close({ message:'close for now', status:'1'})"相同

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Angular 9中,要实现组件调用子组件的方法,可以通过以下步骤进行操作: 1. 在子组件中定义一个公共方法,供组件调用。可以在子组件的.ts文件中添加一个greeting方法,如引用\[1\]和引用\[2\]所示。 2. 在组件的模板文件中,使用子组件的选择器来引入子组件。例如,如果子组件的选择器是`app-child`,则可以在组件的模板文件中使用`<app-child></app-child>`来引入子组件。 3. 在组件的.ts文件中,使用`@ViewChild`装饰器来获取子组件的实例。在组件的类中添加一个ViewChild装饰器,如下所示: ```typescript import { Component, ViewChild } from '@angular/core'; import { ChildComponent } from './child.component'; @Component({ selector: 'app-parent', templateUrl: './parent.component.html', styleUrls: \['./parent.component.css'\] }) export class ParentComponent { @ViewChild(ChildComponent) childComponent: ChildComponent; callChildMethod() { this.childComponent.greeting('John'); } } ``` 在上面的代码中,我们使用`@ViewChild`装饰器来获取子组件的实例,并将其赋值给`childComponent`属性。 4. 在组件的模板文件中,可以通过调用组件的方法来触发子组件的方法。例如,可以在组件的模板文件中添加一个按钮,并在按钮的点击事件中调用组件的方法,如下所示: ```html <button (click)="callChildMethod()">调用子组件方法</button> ``` 当点击按钮时,将会调用组件的`callChildMethod`方法,从而触发子组件的`greeting`方法,并传递参数。 这样,就实现了在Angular 9中组件调用子组件方法的功能。 #### 引用[.reference_title] - *1* [Angular组件——组件调用子组件方法](https://blog.csdn.net/weixin_33938733/article/details/86259262)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [angular-在组件中调用子组件方法](https://blog.csdn.net/weixin_44726310/article/details/103361153)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [angular4中组件如何调用子组件的方法](https://blog.csdn.net/u012396955/article/details/78852140)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值