为了账号安全,请及时绑定邮箱和手机立即绑定

收到第二个控制器未注册的错误

收到第二个控制器未注册的错误

潇湘沐 2022-01-07 19:18:51
我正在尝试从头开始编写一个 Angular 应用程序。(新手)不断收到第二个控制器未注册的错误。为什么?这是我的三个文件,由于某种原因,它一直说“名为“secondController”的控制器未注册。”索引.html<!DOCTYPE html><html ng-app="myApp"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <meta http-equiv="X-UA-Compatible" content="ie=edge">    <title>Document</title>    <script src="./angular.js"></script>    <script src="./secondModule.js"></script>    <script src="./app.js"></script>  </head><body>    <div ng-controller="appController">       <p>App text : {{myAppText}}</p>       <p>secondControllerModule: {{ secondControllerText }}</p>    </div>    <div ng-controller="secondController">        Second Module : {{secondControllerText}}    </div></body></html>应用程序.js angular.module("myApp", []).controller("appController",["$scope", function($scope){    $scope.myAppText = "Hello, I am appController text"}])secondModule.jsangular.module("myApp", []).controller("secondController",["$scope", function($scope){    $scope.secondControllerText = "Hello, I am seasdfsdfasdfacond   Controller Text"}])
查看完整描述

2 回答

?
RISEBY

TA贡献1856条经验 获得超5个赞

我认为根据您的代码,您应该获取三个文件 1. 在 module.js 文件中声明角度模块

var app = angular.module("myApp", []);
  1. 添加第一个控制器 appController.js

    app.controller("appController",["$scope", function($scope){

    $scope.myAppText = "你好,我是 appController text";

    }]);

  2. 添加第二个控制器文件 secondController.js

    app.controller("secondController",["$scope", function($scope){ $scope.secondControllerText = "你好,我是 seasdfsdfasdfacond 控制器文本"; }]);

现在把它们放在这个层次结构中 - 添加 Angular JS 的 ref 然后 1 module.js 2 appController.js 3secondController.js


查看完整回答
反对 回复 2022-01-07
?
交互式爱情

TA贡献1712条经验 获得超3个赞

首先,我想澄清一下这是不正确的:


 <div ng-controller="appController">

           <p>App text : {{myAppText}}</p>


           <p>secondControllerModule: {{ secondControllerText }}</p>// Defined in the wrong ng-controller, 

so move it to the correct one.

        </div>

您收到该错误的原因是您在 js 文件中定义了两次应用程序模块。这很简单,你需要做的就是在你的secondModule.js 中去掉第二个参数


angular.module("myApp")// Needs to be like this


.controller("secondController",["$scope", function($scope){

    $scope.secondControllerText = "Hello, I am seasdfsdfasdfacond   Controller Text"

}])

然后在你的app.js 文件中


angular.module("myApp", [])


.controller("appController",["$scope", function($scope){

    $scope.myAppText = "Hello, I am appController text"


}])

然后在您的 HTML 文件中,您需要修复脚本的顺序


  <script src="./angular.js"></script>

<script src="./app.js"></script> // App module must be first because the module is defined here

  <script src="./secondModule.js"></script>


查看完整回答
反对 回复 2022-01-07
  • 2 回答
  • 0 关注
  • 200 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号