Skip to content

Instantly share code, notes, and snippets.

@chaosim
Last active December 25, 2015 01:19
Show Gist options
  • Save chaosim/6894484 to your computer and use it in GitHub Desktop.
Save chaosim/6894484 to your computer and use it in GitHub Desktop.
why can not find required controller?
module.directive('foo', [
'$log', function($log) {
return {
restrict: 'E',
replace: true,
transclude: true,
template: '<div id="test" ng-transclude></div>',
controller: function($scope) {
this.scope = $scope;
return $scope.panes = [];
},
link: function(scope, element, attrs) {
return $log.log('test1', scope.panes);
}
};
}
]);
module.directive('bar', [
'$log', function($log) {
return {
restrict: 'E',
replace: true,
transclude: true,
require: '^?foo',
controller: function($scope) {
return this.x = 1;
},
template: '<div ng-transclude></div>',
link: function(scope, element, attrs, fooCtrl) {
return $log.log('test2', fooCtrl);
}
};
}
]);
// below is the piece of html:
<foo ng-controller="IndexController">
<bar></bar>
</foo>
// below is the element generated, inspected from chrome developer tools
<div id="test" ng-transclude="" ng-controller="IndexController" class="ng-scope">
<div ng-transclude="" class="ng-scope"></div>
</div>
// below is console output:
test2
Array[0]
length: 0
__proto__: Array[0]
angular.js:5930
test1
Array[0]
length: 0
__proto__: Array[0]
@atian25
Copy link

atian25 commented Oct 9, 2013

帮你提交到这, 好分析点: http://plnkr.co/edit/aM81utpVPwYoI6ru28rT?p=preview
不过没看懂你想做什么。
this.scope = $scope;return this.x = 1; 这两句代码干嘛的? 为啥要用this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment