2015-10-28 118 views
1

这是我第一次尝试使用Angular.js做一些其他的工作,除了流程教程。我开始从http://www.w3schools.com/angular/angular_application.asp的骨架应用程序。为了我想要做的事情,我已经微调了一下,它只是显示了我的REST API中GET方法的结果。所以我有这样的myAppCtrl.js:

app.controller("myAppCtrl", function($scope,$http) { 
    $scope.message = ""; 
    $scope.hosts = ""; 
    $scope.left = function() {return 100 - $scope.message.length;}; 
    $scope.clear = function() {$scope.message = "";}; 
    $scope.save = function() {alert("Note Saved");}; 
    $scope.getHosts = function() { 
     $http.get("http://172.17.0.5:3000/user").success(function(response) {$scope.hosts = response.records;}); 
    }; 
}); 

而且我有这个myApp.js文件:

var app = angular.module("myApp", []); 

但它不工作我得到了我的克罗默这些错误JavaScript控制台:

Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting! 
Watchers fired in the last 5 iterations: [] 
http://errors.angularjs.org/1.3.14/$rootScope/infdig?p0=10&p1=%5B%5D 
    at angular.js:63 
    at Scope.$digest (angular.js:14281) 
    at Scope.$apply (angular.js:14506) 
    at bootstrapApply (angular.js:1448) 
    at Object.invoke (angular.js:4185) 
    at doBootstrap (angular.js:1446) 
    at bootstrap (angular.js:1466) 
    at angularInit (angular.js:1360) 
    at angular.js:26176 
    at HTMLDocument.trigger (angular.js:2744)(anonymous function) @ angular.js:11607(anonymous function) @ angular.js:8557Scope.$apply @ angular.js:14508bootstrapApply @ angular.js:1448invoke @ angular.js:4185doBootstrap @ angular.js:1446bootstrap @ angular.js:1466angularInit @ angular.js:1360(anonymous function) @ angular.js:26176trigger @ angular.js:2744eventHandler @ angular.js:3014 
angular.js:63 Uncaught Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting! 
Watchers fired in the last 5 iterations: [] 
http://errors.angularjs.org/1.3.14/$rootScope/infdig?p0=10&p1=%5B%5D 

然后过了一段时间我得到这个错误太:

GET http://172.17.0.5:3000/user net::ERR_CONNECTION_TIMED_OUT(anonymous function) @ angular.js:9827sendReq @ angular.js:9628serverRequest @ angular.js:9344processQueue @ angular.js:13189(anonymous function) @ angular.js:13205Scope.$eval @ angular.js:14401Scope.$digest @ angular.js:14217Scope.$apply @ angular.js:14506bootstrapApply @ angular.js:1448invoke @ angular.js:4185doBootstrap @ angular.js:1446bootstrap @ angular.js:1466angularInit @ angular.js:1360(anonymous function) @ angular.js:26176trigger @ angular.js:2744eventHandler @ angular.js:3014 
angular.js:11607 Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting! 
Watchers fired in the last 5 iterations: [] 
http://errors.angularjs.org/1.3.14/$rootScope/infdig?p0=10&p1=%5B%5D 
    at angular.js:63 
    at Scope.$digest (angular.js:14281) 
    at Scope.$apply (angular.js:14506) 
    at done (angular.js:9659) 
    at completeRequest (angular.js:9849) 
    at XMLHttpRequest.requestError (angular.js:9800)(anonymous function) @ angular.js:11607(anonymous function) @ angular.js:8557Scope.$apply @ angular.js:14508done @ angular.js:9659completeRequest @ angular.js:9849requestError @ angular.js:9800 

UPDATE:这是我的看法。我想:

<html ng-app="myApp"> 
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js"></script> 
<!-- switch back to angular.min.js after debigging --> 
<body> 

<div ng-controller="myAppCtrl"> 

<h2>My Note</h2> 

<p><textarea ng-model="message" cols="40" rows="10"></textarea></p> 

<p> 
<button ng-click="save()">Save</button> 
<button ng-click="clear()">Clear</button> 
</p> 

<p>Number of characters left: <span ng-bind="left()"></span></p> 
<p>hosts: <span ng-bind="getHosts()"></span></p> 
</div> 

<script src="myApp.js"></script> 
<script src="myAppCtrl.js"></script> 

</body> 
</html> 

我在做什么错?

+1

你的观点是什么样子?你是否在渲染阶段改变模型? – Leromul

+0

为什么不使用'$ .get()'? – la1ch3

+0

'http://172.17.0.5:3000/user'是否处于工作状态?也显示你的html代码 –

回答

1

我想说的问题是,getHosts()执行每个摘要周期。当HTTP请求解析时,它会触发另一个摘要循环,从而导致无限循环。

试试这个在您的控制器

$scope.message = ""; 
$scope.hosts = ""; 
// etc, no need for a getHosts function 

$http.get("http://172.17.0.5:3000/user").then(function(response) { 
    $scope.hosts = response.data.records; 
}); 

,而不是在你的模板

<p>hosts: <span ng-bind="hosts"></span></p> 
+0

这照顾了我的一些问题。当我尝试联系http://172.17.0.5:3000/user时,仍然收到ERR_CONNECTION_TIMED_OUT,但我想我的浏览器无法访问。我怀疑http://172.17.0.5:3000/user只能从Web服务器访问。我可能不得不建立某种代理。 –

1

它看起来像你被绑定到视图您getHosts()函数中改变模型。

当您第一次加载页面时,会调用getHosts(),然后调用您的API并更新$scope.hosts并显示结果。由于$ scope.hosts被更新,它会触发一个新的渲染器,从而导致另一个API调用,从而导致循环(因此'10 $ digest()迭代达到'错误)。

在您看来,您看起来并不像您使用的是$scope.hosts

因此,首先用ng-bind="hosts"替换ng-bind="getHosts()",然后在控制器添加以下内容:

$http.get("http://172.17.0.5:3000/user").success(function(response) { 
    $scope.hosts = response.records; 
}); 
1

问题是这里

<p>Number of characters left: <span ng-bind="left()"></span></p> 
<p>hosts: <span ng-bind="getHosts()"></span></p> 

的NG-绑定= “表达” 是等于{{表达式}},并且你不能把表达式等同于一个函数。

我建议加NG-变化read about it here在textarea标签,并把它等同于计算左侧的功能,并且还GETHOST像下面这样:

​​

,并在控制器:

$scope.refresh = function() { 
    $scope.left = 100 - $scope.message.length; 
    $http.get("http://172.17.0.5:3000/user").success(function(response) {$scope.hosts = response.records;}); 
}; 
相关问题