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) {
}
}
}]);
})
页面:
错误:

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
我发现要包含才行。
其实我想实现的是,
组件1:查询编辑框、查询按钮
组件2:查询列表
但我发现异步查询的结果想转入组件2,要不用公共变量,要不用监听,要不就广播,都不是很好耦合较强。
最好是传参给事件之类的,然后自动渲染列表。不知道能不能实现呢?