angularjs控制器执行两次 [英] angularjs controller executes twice
问题描述
有一个类似的问题被问<一个href=\"http://stackoverflow.com/questions/15535336/combating-angularjs-executing-controller-twice\">here但它并没有帮助我。
我学习angularjs,我注意到控制器执行两次。
我有一个非常简单的例子小提琴,显示了行为 rel=\"nofollow\">
我建的例子,因为我是学习服务,起初我还以为是服务到控制器的注入,但我评论所有相关的服务和仍然控制器的执行两次code
我的例子的作品,但我怕我做错了什么。
&LT; DIV NG-应用=MyApp的&GT;
&LT; DIV NG控制器=MyCtrl&GT;
{{数据1}}
&LT; / DIV&GT;
&LT; / DIV&GT;VAR应用= angular.module('MyApp的',[])
app.service('服务1',函数(){
返回{
ajxResponse1:dataFromService1
};
});功能MyCtrl($范围,服务1){
警报('进入MyCtrl');
$ scope.data1 = Service1.ajxResponse1;
警报('退出MyCtrl');
}
一个可能的来源是这样的:如果你正在使用路由和指定路线控制器 - 你不能在该路由使用模板指定。我们有这样的问题,当这个被忽略。
A similar question was asked here but it did not help me.
I am learning angularjs and I noticed the controller is executed twice.
I have a very simple fiddle example that shows the behavior here
I built the example as I was learning about services and at first I thought it was the injecting of the services into the controller but I commented all the code related to the services and still the controller is executed twice.
My example works but I am afraid I am doing something wrong.
<div ng-app="MyApp">
<div ng-controller="MyCtrl">
{{data1}}
</div>
</div>
var app = angular.module('MyApp', [])
app.service('Service1', function(){
return {
ajxResponse1: 'dataFromService1'
};
});
function MyCtrl($scope, Service1){
alert('Entering MyCtrl');
$scope.data1 = Service1.ajxResponse1;
alert('Exiting MyCtrl');
}
One possible source is this: if you are using Routing and specify the controller in routes - you must not specify it in template that the route uses. We had that problem when this was overlooked.
这篇关于angularjs控制器执行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!