javascript - requirejs写组件,两个组件在同一页面使用的,但组件1使用require属性获取不到组件2的ctrl到link里面呢?
天蓬老师
天蓬老师 2017-04-11 12:03:52
[JavaScript讨论组]

1、我使用requirejs方式编写directive,两个组件在同一页面使用的,但组件1使用require属性获取不到组件2的controller到link里面呢?
2、
组件1:

define(['app','ModuleService','ParamFactory','ProductListsFactory'],function (app) {
  app.directive('headerSearch',['ModuleService','$rootScope','ParamFactory','$ionicLoading','ProductListsFactory',
    function (module,$rootScope,ParamFactory,$ionicLoading,ProductListsFactory) {
    return {
      restrict:'AE',
     scope:{tdSearch:'&',keywords:'@'},
      templateUrl:'module/headerSearch/headerSearch.html',
      require:'?^productItem',
      link:function (scope, element, attrs,ctrl) {
        console.log('---------------------------');
        console.log(ctrl);
        console.log('===========================');
      },
      controllerAs:'headerSearchCtrl'
    }
  }]);
});

组件2:

define(['app','ModuleService'],function (app) {
   app.directive('productItem',['ModuleService',function (module) {
    return {
      restrict:'E',
     scope:{lists:'@',module:'@'},
      link:function (scope, element, attrs) {
      },
      templateUrl:'module/productItem/productItem.html',
      controller:function ($scope) {

      }
    }
  }]);
})

页面:



  
  


错误:

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(1)
阿神
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="lib/angular/angular.js"></script>
</head>
<body ng-app="app">
<p ng-controller="ctrl1">

  <!--这样写不行
  <d1>1111</d1>
  <common></common>-->
  <common>
    <d1>1111</d1>
  </common>
</p>

<script>
  var app = angular.module('app',[]);
  app.controller('ctrl1',function ($scope) {
    console.log('ctrl1');
  });
  app.directive('common',function(){
    return {
      controller: function($scope){
      this.method1 = function(){
        console.log('method1');
      };
      this.method2 = function(){
        console.log('method2');
      };
    }
  }
  });

  app.directive('d1',function(){
    return {
      require: '?^common',
      link: function(scope,elem,attrs,common){
        console.log(common);
      scope.method1 = common.method1;
    }
  }
  });

</script>

</body>
</html>

我发现要包含才行。

其实我想实现的是,
组件1:查询编辑框、查询按钮
组件2:查询列表
但我发现异步查询的结果想转入组件2,要不用公共变量,要不用监听,要不就广播,都不是很好耦合较强。
最好是传参给事件之类的,然后自动渲染列表。不知道能不能实现呢?

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号