【问题标题】:AngularJS infinite loop on scope function call范围函数调用上的AngularJS无限循环
【发布时间】:2018-10-16 09:51:09
【问题描述】:

我是 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)

代码: 应用.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>

那我做错了什么?

【问题讨论】:

  • 在您的控制器端调用函数只需将 jsondata 打印到 html 端
  • 不能解决问题
  • 错误是什么?

标签: html angularjs angularjs-scope


【解决方案1】:

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

注意:ng-init 只触发一次

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 2014-01-26
    • 2023-03-22
    相关资源
    最近更新 更多