2017-02-25 81 views
0

我是AngularJS的新手,开始于2天前,但我无法解决此问题。AngularJS - 我的ng控制器的名称找不到

The JS Console Error here

看来,我的指令NG控制器无法找到我的控制器。

这里是我的代码:

<section class="index" ng-app="indexApp" ng-controller="cRegister"> 
 
    <form> 
 
    <input type="text" ng-model="username" /> 
 
    <input type="password" ng-model="password" /> 
 
    <input type="submit" ng-click="" /> 
 
    </form> 
 

 
    <script> 
 
    var app = angular.module('indexApp', []) 
 
    app.controller('cRegister', ['$scope', function() { 
 
     console.log('controller: true'); 
 
    }]); 
 
    </script> 
 
</section>

(这是主要的网页的一部分,我用NG-视图中显示):

<html ng-app="web"> 
 

 
<head> 
 
    <title>AngularJS, tries</title> 
 
    <meta charset="utf-8"> 
 
</head> 
 

 
<body> 
 
    <div ng-view></div> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min.js"></script> 
 
    <script src="https://code.angularjs.org/1.6.2/angular-route.min.js"></script> 
 
    <script> 
 
    var app = angular.module('web', ['ngRoute']); 
 
    app.config(function($routeProvider) { 
 
     $routeProvider 
 
     .when('/', { 
 
      templateUrl: 'partials/index.php' 
 
     }) 
 
     .otherwise({ 
 
      redirectTo: '/' 
 
     }) 
 
    }); 
 
    </script> 
 
</body> 
 

 
</html>

希望有人能帮助我,谢谢! :D

+0

在div的子元素中添加ng-controller包含ng-app属性 –

回答

3

你不能在一个html中有两个ng-app指令。 删除内部的一个,并将该控制器添加到第一个ng-app

+0

谢谢您的回答!但是我不能让索引部分的app.controller()因此:/ – xBen

+0

在javascript中创建另一个角度模块并将其注入到索引模块中,但是dnt使用ng-app两次 – pranavjindal999

+1

工作!谢谢 ! – xBen