为什么需要为子控制器的 $scope 属性定义一个属性?
HTML 元素上的这个 ng-class 有效:
app.controller("mainController",['$anchorScroll','$scope','$location',function($anchorScroll,$scope,$location){
$scope.forms={};
}]);
app.controller("modalController",['$scope',function($scope){
$scope.forms.contactForm = false;
$scope.forms.toggleContactForm = function(){
$scope.forms.contactForm = !$scope.forms.contactForm;
}
}]);
<body ng-controller="mainController">
<div ng-class="{hidden:!forms.contactForm}" id="contactForm" ng-controller="modalController"></div>
</body>
这个 ng-class 不会:
app.controller("mainController",['$anchorScroll','$scope','$location',function($anchorScroll,$scope,$location){
}]);
app.controller("modalController",['$scope',function($scope){
$scope.contactForm = false;
$scope.toggleContactForm = function(){
$scope.contactForm = !$scope.contactForm;
}
}]);
<body ng-controller="mainController">
<div ng-class="{hidden:!contactForm}" id="contactForm" ng-controller="modalController"></div>
</body>