我有一个应用程序,我正在建设角度,我有大约8-10视图建立。 所有的视图都有一个共同的页脚,基于视图和我需要有条件地显示/隐藏页脚上的一些内容的一组业务规则。AngularJS:ng-include和ng-controller
所以。 我有控制器为每个视图,然后一个脚注。 我使用ng-include包含了常见的页脚布局,其中我包括的html引用了ng控制器中的页脚控制器。
的Index.html
<body ng-controller="MainCtrl as vm">
<p>Message from Main Controller '{{vm.mainMessage}}'</p>
<div ng-include="'commonFooter.html'"></div>
</body>
commonFooter.html
<div ng-controller="FooterCtrl as vm">
<p>Message from Footer Controller '{{vm.message}}'</p>
<p ng-show="vm.showSomthing">Conditional footer Content</p>
</div>
我希望每个视图控制器来确定页脚的状态和具体内容是否被隐藏。 (以下shouldDisplaySomthingInFooter)
app.controller('MainCtrl', function($scope) {
var vm = this;
vm.mainMessage= 'HEELO';
vm.shouldDisplaySomthingInFooter = true;
window.console.log('Main scope id: ' + $scope.$id);
});
然后我本来打算,在FooterController将达到回父控制器和拔出具体设置以使/基于业务规则禁止的内容。
app.controller('FooterCtrl', function($scope) {
var vm = this;
vm.message = 'vm footer';
window.console.log('Footer scope id: ' + $scope.$id);
window.console.log('Footer parent scope id: ' + $scope.$parent.$id);
window.console.log('Footer grandparent scope id: ' + $scope.$parent.$parent.$id);
window.console.log('Footer grandparent scope name: ' + $scope.$parent.$parent.mainMessage);
window.console.log('Footer grandparent scope condition: ' + $scope.$parent.$parent.shouldDisplaySomthingInFooter);
vm.showSomthing = false; //how to pull from parent scope to bind the ng-show to a value set in the parent from within a ng-include?
});
我这里有这个例子: http://plnkr.co/edit/ucI5Cu4jjPgZNUitY2w0?p=preview
什么我发现是我,当我到达到父范围,拉出它是回来为未定义的内容,我不知道为什么。
我可以看到范围通过检查scopeid嵌套到祖父级别,我相信这是因为ng-include在视图范围下面添加了一个额外的范围层。
加分:如果我不能使用$ scope对象,并且可以坚持使用var vm = this;
这样做的方式,那将是更可取的。但乞丐不能选择器:)
app.controller('MainCtrl', function($scope) {
var vm = this;
非常感谢您提前。