[英]Controller constructor called more than once
I have a simple controller 我有一个简单的控制器
angular.module('datahubApp')
.controller('LoginController',[ '$scope' , '$resource', LoginController]);
function LoginController($scope,$resource){
console.log('Constructor called');
}
I have used the this in HTML in 2 places for two different components, in the same view 我在同一个视图的两个地方的HTML中使用了this,用于两个不同的组件
<div ng-controller="Logincontroller as ln"> </div>
<div ng-controller="Logincontroller as ln"> </div>
Now when I run the app, i see this in console 现在,当我运行该应用程序时,我会在控制台中看到它
Constructor Called
Constructor Called
Constructor Called
Question 1: 问题1:
So, this is getting called thrice. 因此,这被称为三次。 Is this normal. 这正常吗
Question 2: In that case, how can I pass the scope variable between components? 问题2:在那种情况下,如何在组件之间传递范围变量?
Why are you using same controller two times in a view. 为什么在视图中两次使用同一控制器。 Use it one time only and it would work perfect. 仅使用一次,它将完美工作。
You can use like: 您可以像这样使用:
<div ng-controller="Logincontroller as ln">
<div></div> <!-- Your first div -->
<div> </div> <!-- Your second div -->
</div>
AngularJS will call a controller every time it found in a view. 每当在视图中找到AngularJS时,它将调用一个控制器。 No matter how much time is there in view part. 无论视图中有多少时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.