情况如下:
这是 JS 文件 main.js 和 HTML 文件 index.html 的片段:
angular.module("myApp", []);
angular.module("myApp", []).controller("postController", function($scope) {
$scope.buttonName = "Post";
})
angular.module("myApp", []).controller("myController", function($scope) {
$scope.ButtonStatus = "OFF";
$scope.myStyle = buttonGreen //{'background-color':'#00FF00', 'font-size': '24px', 'width': '80px'}
$scope.toggleRelay = function(){
if($scope.ButtonStatus == "OFF")
{
$scope.ButtonStatus = "ON";
$scope.myStyle = buttonRed;
}
else
{
$scope.ButtonStatus = "OFF";
$scope.myStyle = buttonGreen;
}
}
})
<body ng-app="myApp">
<h1 id="the-header">Wellcome to home electricity manager!</h1>
<div add-row ng-controller="myController" style="text-align: center; display: inline-block;">
<span style="white-space:pre;">Button text</span><br/>
<button id="first-button" ng-style="myStyle" ng-click="toggleRelay()" id="switch-cirquit-1">{{ButtonStatus}}</button>
</div>
<div add-row ng-controller="myController" style="text-align: center; display: inline-block;">
<span id="second-button" style="white-space:pre;">{{buttonOneText}}</span><br/>
<button ng-style="myStyle" ng-click="toggleRelay()" id="switch-cirquit-1">{{ButtonStatus}}</button>
</div>
<div ng-controller="postController" style="text-align: center; display: inline-block;">
<button>ASSS</button>
</div>
</body>
运行后我得到 this错误。这个想法是将两个 Controller 放在同一个应用程序下,但似乎并不能那样工作。如果我删除 postController
一切正常。我在这里阅读了如何在同一个应用程序中使用两个 Controller ,例如但这个方法似乎对我不起作用。
最佳答案
您不需要再次声明具有空依赖项的模块,试试这个
angular.module("myApp", []);
angular.module("myApp").controller("postController", function($scope) {
$scope.buttonName = "Post";
})
angular.module("myApp").controller("myController", function($scope) {
}
关于javascript - 两个 Controller 相同的应用程序,出现错误未注册 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47992372/