2017-05-28 118 views
0

我创建了一个新项目,但遇到了一个问题,我无法修复。“控制器”未注册angularjs

这里是我的例子:

我有这样的错误:

“错误:[$控制器:ctrlreg]名为 'PostController的' 控制器未注册”。

Connexion.html

<section id="login" ng-controller="PostController as postCtrl"> 
<div class="container center-bloc"> 
    <div class="row"> 
     <div class="col-xs-12"> 
      <div class="form-wrap"> 
       <div class="text-center"> 
        <img id="logo" style="max-width:100%;" src="img.png"> 

       </div> 
       <h2 id="ResultConnexion"></h2> 
       <form role="form" id="login-form" ng-submit="postCtrl.Search1()" method="POST" autocomplete="off"> 
        <div class="form-group"> 
         <input id="userLogin" required autofocus ng-model="postCtrl.inputData.username" type="text" name="user" id="email" class="form-control" placeholder="[email protected]"> 
        </div> 
        <div class="form-group"> 
         <input id="userPassword" required ng-model="postCtrl.inputData.password" type="password" name="mdp" id="key" class="form-control" placeholder="Password"> 
        </div> 
        <button type="submit" class="btn btn-custom btn-lg btn-block" ng-disabled="login.$invalid" >Connexion</button> 
        <div class="alert alert-danger" ng-show="errorMsg"> 
         <button type="button" class="close" data-dismiss="alert" aria-hidden="true"> 
          ×</button> 
         <span class="glyphicon glyphicon-hand-right"></span>&nbsp;&nbsp;{{errorMsg}} 
        </div> 
        <div></div> 
       </form> 
       <hr> 
      </div> 

     </div> 
    </div> 
</div> 

索引。 js

'use strict'; 
var routeApp = angular.module("routeApp", ["ngRoute"]); 
routeApp.config(function($routeProvider) { 
$routeProvider 
    .when("/", { 
     url: "/", 
     templateUrl : "template/connexion.html", 
     controller : "PostController" 
    }) 

}); 






var routeAppControllers = angular.module('routeAppControllers',['ngRoute']); 



routeAppControllers.controller('PostController', ['$scope', '$http', function($scope, $http) { 


this.postForm = function() { 



    var Pass = {"user":(this.inputData.username),"mdp":(this.inputData.Password)}; 

    $http({ 
     method: 'POST', 
     url: '/', 
     data: Pass, 
     headers: {'Content-Type': 'application/json'} 
    }) 

     .then(function(response) { 


      if (response.data.resultat == 0) { 

       window.location.href = '/information'; 
       alert('hello') 

      } else { 
       $scope.errorMsg = 'Bad'; 
      } 
     }, function(response) { 
      // called asynchronously if an error occurs 
      // or server returns response with an error status. 
     }) 
} 

}]); 

任何人都可以帮忙吗?

回答

0

我想你应该从模板中删除ng-controller="PostController as postCtrl">,因为你在路由中定义了它。并且改变到下面。

.when("/", { 
    url: "/", 
    templateUrl : "template/connexion.html", 
    controller : "PostController as postCtrl" 
}) 

是建议一个小窍门声明var vm = this和使用的vm代替this

vm.postForm = function() {...} 
+0

我测试了两种解决方案,但没有工作的多个控制器实例 – Gazano

1

你有两个独立的模块,并显示在没有其他注入。

假设你ng-app使用你需要注入routeAppControllers模块为routeApp模块

var routeApp = angular.module("routeApp", ["ngRoute", "routeAppControllers"]); 

routeApp你也只需要注入ngRoute一次。

正如其他答案提到你只用ng-controller当它不是路由配置也宣布,或者你最终

相关问题