2

子コントローラーの $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>
4

1 に答える 1