AngularJS 作用域函数调用无限循环
AngularJS infinite loop on scope function call
我是 AngularJS programming.So 的新手 我在我的 html 中调用了 getDevice() 范围函数,我似乎进入了无限 loop.Console 日志:
angular.js:14800 Error: [$rootScope:infdig] http://errors.angularjs.org/1.6.9/$rootScope/infdig?p0=10&p1=%5B%5D
at angular.js:88
at m.$digest (angular.js:18418)
at m.$apply (angular.js:18649)
at l (angular.js:12627)
at t (angular.js:12871)
at XMLHttpRequest.A.onerror (angular.js:12799)
代码:
App.js
var myApp = angular.module('myApp', []);
myApp.controller('myController', function($scope, $location, $http)
{
$scope.getDevice = function(deviceId)
{
$http.get('http://localhost:8080/client/' + deviceId + '/receive/device')
.then(function (response)
{
$scope.jsondata = response.data;
})
console.log("working");
};
});
html
<body>
<div ng-app="myApp" ng-controller="myController">
{{getDevice("elsysDev")}}
</div>
</body>
那我做错了什么?
如果您真的想在视图中调用该方法,请使用 ng-init
Note: ng-init
is fired only once
<body>
<div ng-app="myApp" ng-controller="myController" ng-init="getDevice('elsysDev')">
</div>
</body>
我是 AngularJS programming.So 的新手 我在我的 html 中调用了 getDevice() 范围函数,我似乎进入了无限 loop.Console 日志:
angular.js:14800 Error: [$rootScope:infdig] http://errors.angularjs.org/1.6.9/$rootScope/infdig?p0=10&p1=%5B%5D
at angular.js:88
at m.$digest (angular.js:18418)
at m.$apply (angular.js:18649)
at l (angular.js:12627)
at t (angular.js:12871)
at XMLHttpRequest.A.onerror (angular.js:12799)
代码: App.js
var myApp = angular.module('myApp', []);
myApp.controller('myController', function($scope, $location, $http)
{
$scope.getDevice = function(deviceId)
{
$http.get('http://localhost:8080/client/' + deviceId + '/receive/device')
.then(function (response)
{
$scope.jsondata = response.data;
})
console.log("working");
};
});
html
<body>
<div ng-app="myApp" ng-controller="myController">
{{getDevice("elsysDev")}}
</div>
</body>
那我做错了什么?
如果您真的想在视图中调用该方法,请使用 ng-init
Note:
ng-init
is fired only once
<body>
<div ng-app="myApp" ng-controller="myController" ng-init="getDevice('elsysDev')">
</div>
</body>