无法让第二个控制器在Angular SPA上工作

时间:2016-03-07 22:09:46

标签: javascript angularjs

我有两个页面 - signup pagelogin page。我现在想要将两个表单集成到我的home page上。我在HomepageModule.js中添加了对两个控制器的引用,但注册功能不起作用。这是我的档案,我做错了什么?

Homepage.ejs:

<body ng-app="HomepageModule" ng-cloak>
    <form ng-submit="submitLoginForm()" ng-controller="HomepageController">
        <!-- Some Inputs -->
    </form>
   <div ng-controller="SignupController"
    <form ng-submit="submitSignupForm()" id="sign-up-form" class="form-signin" name="signup">
        <!-- Some Inputs -->
    </form>
   </div>
</body>

HomepageModule:

(function() {
  'use strict';
  angular.module('HomepageModule', ['toastr', 'compareTo','ngMaterial'])
    .controller('HomepageController', function($scope) {
      $scope.isOpen = false;
      $scope.demo = {
        isOpen: false,
        count: 0,
        selectedDirection: 'left'
      };
    })
    .controller('SignupController', function($scope) {
      $scope.isOpen = false;
      $scope.demo = {
        isOpen: false,
        count: 0,
        selectedDirection: 'left'
      };
    });
})();

HomepageController.js:

angular.module('HomepageModule').controller('HomepageController', ['$scope', '$http', 'toastr', function($scope, $http, toastr){

    $scope.loginForm = {
        loading: false
    }

    $scope.submitLoginForm = function (){
    }
    //other app logic
}]);

SignupController.js:

angular.module('HomepageModule').controller('SignupController', ['$scope', '$http', 'toastr', function($scope, $http, toastr){

    // set-up loading state
    $scope.signupForm = {
        loading: false
    }

    $scope.submitSignupForm = function(){
    }
        //other app logic
    }]);

编辑:

我最怀疑的地方是angular.module('HomepageModule').controller('SignupController', ...行,是否应该从两个控制器中引用HomepageModule?另外,在模块中包含两个控制器的语法是否正确?

2 个答案:

答案 0 :(得分:0)

您需要以注册表单<form ng-submit="submitSignupForm()" ng-controller="SignupController" ...

指定ng-controller

答案 1 :(得分:0)

这将允许两个控制器存在。现在,页面上只存在HompageController。

 <body ng-app="HomepageModule" ng-cloak>
    <form ng-submit="HC.submitLoginForm()" ng-controller="HomepageController as HC">
        <!-- Some Inputs -->
    </form>
    <form ng-submit="SC.submitSignupForm()" ng-controller="SingupController as SC" id="sign-up-form" class="form-signin" name="signup">
        <!-- Some Inputs -->
    </form>
</body>