component值require

作用

directive不同,componentrequire跟的是一个配置对象

  • 引用父级组件的控制器 ,可以拿到父级组件的属性与方法与方法
  • 使用的时候,我们一般在钩子函数$onInit中创建一个变量来进行保存引用的控制器

例子

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <script src="https://cdn.bootcdn.net/ajax/libs/angular.js/1.6.8/angular.min.js"></script>
  </head>
  <body>
    <div ng-app="myApp" ng-controller="myCtrl">
      <h1>{{indexName}}</h1>
      <!-- 自定义组件 -->
      <component-one>
        <component-two></component-two>
      </component-one>
    </div>

    <script>
      var app = angular.module("myApp", []);
      app.controller("myCtrl", function ($scope) {
        $scope.indexName = "主页";
        $scope.childName = "我是传到组件中的信息";
      });
      app.component("componentOne",{
        controller:function(){
          this.name = "component-one"
          this.sayHello = function(){
            console.log("Hello");
          }
        }
      })
      app.component("componentTwo",{
        template:`
            <div>
                <h3>自定义组件</h3>
            </div>
        `,
        require:{
          one:"?^componentOne"
        },
        controller:function(){
          this.$onInit = function(){
            console.log(this.one.name);
            this.one.sayHello();
          }
        }
      });

    </script>
  </body>
</html>
  • 效果

在这里插入图片描述

关于?^的说明,可以相见directive的require说明,链接,与directive的require不同的是取

  • require会将引入的controller传入link函数的第四个参数,如果加上了问号就会为null,不加就会报错
  • directive的require是当我们使用的时候,如果没有当前的controller,加上了?,我们取到的值为null,没加?就会报错,如以下例子在这里插入图片描述
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <script src="https://cdn.bootcdn.net/ajax/libs/angular.js/1.6.8/angular.min.js"></script>
  </head>
  <body>
    <div ng-app="myApp" ng-controller="myCtrl">
      <h1>{{indexName}}</h1>
      <!-- 自定义组件 -->
      <component-one>
        <component-two></component-two>
      </component-one>
    </div>

    <script>
      var app = angular.module("myApp", []);
      app.controller("myCtrl", function ($scope) {
        $scope.indexName = "主页";
        $scope.childName = "我是传到组件中的信息";
      });
      app.component("componentOne",{
        controller:function(){
          this.name = "component-one"
          this.sayHello = function(){
            console.log("Hello");
          }
        }
      })
      app.component("componentTwo",{
        template:`
            <div>
                <h3>自定义组件</h3>
            </div>
        `,
        require:{
          one:"?^componentOne",
          three:"^componentThree"
        },
        controller:function(){
          this.$onInit = function(){
            console.log(this.one.name);
            this.one.sayHello();
            console.log(this.three);
          }
        }
      });

    </script>
  </body>
</html>
  • 如果是从自身父级开始找,那么我们可以直接简写为
{
	require:{
		componentOne:"^^"
	}
}
  • 例子
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <script src="https://cdn.bootcdn.net/ajax/libs/angular.js/1.6.8/angular.min.js"></script>
  </head>
  <body>
    <div ng-app="myApp" ng-controller="myCtrl">
      <h1>{{indexName}}</h1>
      <!-- 自定义组件 -->
      <component-one>
        <component-two></component-two>
      </component-one>
    </div>

    <script>
      var app = angular.module("myApp", []);
      app.controller("myCtrl", function ($scope) {
        $scope.indexName = "主页";
        $scope.childName = "我是传到组件中的信息";
      });
      app.component("componentOne",{
        controller:function(){
          this.name = "component-one"
          this.sayHello = function(){
            console.log("Hello");
          }
        }
      })
      app.component("componentTwo",{
        template:`
            <div>
                <h3>自定义组件</h3>
            </div>
        `,
        require:{
          componentOne:"^"
        },
        controller:function(){
          this.$onInit = function(){
            console.log(this.componentOne.name);
            this.componentOne.sayHello();
          }
        }
      });

    </script>
  </body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值