AngularJS在范围函数调用上无限循环

时间:2018-05-06 12:41:49

标签: html angularjs angularjs-scope

我是AngularJS编程的新手。所以我在我的html中调用了getDevice()范围函数,我似乎进入无限循环。控制日志:

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>

那么我做错了什么?

1 个答案:

答案 0 :(得分:1)

如果您真的想在视图中调用该方法,请使用ng-init

  

注意:ng-init只会被触发一次

<body>
     <div ng-app="myApp" ng-controller="myController" ng-init="getDevice('elsysDev')"> 
     </div>
</body>