Angularjs $ rootScope:infdig在视图方法中调用http时出错

时间:2018-07-12 10:10:57

标签: angularjs angularjs-scope angularjs-http

我正在尝试从AngularJS应用的html页面中的服务器获取一些信息。但是,当我使用$ scope在html文件中调用该函数时,该方法本身工作正常。我收到$ rootScope:infidg错误。

控制器中的方法:

    $scope.getTranslation = function(){
        $http.get('https://producthero.com/index.php?option=com_hero&task=language.translate&s=SEARCH')
            .then(
                function (response) {
                    return response.data.translation;
                }
            );
    };

使用ng-app和ng-controller调用html文件:

<div ng-controller="Product">
    <span>{{getTranslation()}}</span>
</div>

我正在使用这种翻译方式,因为该网站的初始后端在Joomla中运行,我知道i18n,但是我们不能在这里使用它。

错误是:

  

http://errors.angularjs.org/1.6.4/$rootScope/infdig?p0=10&p1=%5B%5D

angular.min.js:123 Error: [$rootScope:infdig] <http://errors.angularjs.org/1.6.4/$rootScope/infdig?p0=10&p1=%5B%5D>
    at angular.min.js:6
    at m.$digest (angular.min.js:147)
    at m.$apply (angular.min.js:149)
    at angular.min.js:21
    at Object.invoke (angular.min.js:44)
    at c (angular.min.js:21)
    at Sc (angular.min.js:22)
    at ue (angular.min.js:20)
    at HTMLDocument.<anonymous> (angular.min.js:331)
    at i (jquery.min.js:2)

我希望这只是我的愚蠢,并且我缺少使使用HTTP进行这种直接调用的东西!

编辑:

以下是我对翻译问题的解决方案(感谢@Aleksey Solovey的回答):

控制器方法

$scope.translations = {};

$scope.getTranslation = function(string){
    $http.get('https://producthero.com/index.php?option=com_hero&task=language.translate&s=' + string)
        .then(
            function (response) {
                $scope.translations[string] = response.data.translation;
            });
    };

查看通话

<div ng-app="products">

        <div ng-controller="Product">
            <span ng-init="getTranslation('SEARCH')">{{translations.SEARCH}}</span>
    </div>
</div>

2 个答案:

答案 0 :(得分:1)

$http请求将返回Promise,而不是某些值。因此,您需要先填充一个有作用域的变量,然后(异步)使用它。它应该是这样的:

var app = angular.module('myApp', []);
app.controller('Product', function($scope, $http) {
  $scope.getTranslation = function() {
    $http.get('https://producthero.com/index.php?option=com_hero&task=language.translate&s=SEARCH').
    then(function(response) {
      $scope.translation = response.data.translation;
    });
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>

<div ng-app="myApp">
  <div ng-controller="Product">
    <span ng-init="getTranslation()">{{translation}}</span>
  </div>
</div>

答案 1 :(得分:0)

  

您能否给出一个不使用ng-init的示例的其他答案?

只需在控制器中手动对其进行初始化:

app.controller('Product', function($scope, $http) {
  $scope.getTranslation = function() {
    $http.get('https://producthero.com/index.php?option=com_hero&task=language.translate&s=SEARCH').
    then(function(response) {
      $scope.translation = response.data.translation;
    });
  };
  //INSTEAD OF ng-init
  $scope.getTranslation();
});
<div ng-app="myApp">
  <div ng-controller="Product">
    ̶<̶s̶p̶a̶n̶ ̶n̶g̶-̶i̶n̶i̶t̶=̶"̶g̶e̶t̶T̶r̶a̶n̶s̶l̶a̶t̶i̶o̶n̶(̶)̶"̶>̶{̶{̶t̶r̶a̶n̶s̶l̶a̶t̶i̶o̶n̶}̶}̶<̶/̶s̶p̶a̶n̶>̶
    <span>{{translation}}</span>
  </div>
</div>

可以滥用ng-init指令在模板中添加不必要的逻辑量。 ngInit只有少数适​​当的用途。参见AngularJS ng-init API Reference


出于性能原因,应避免在带有{{ }}的HTML插值绑定中使用函数。这些功能在每个摘要循环中被调用一次或多次。

  

错误

<div ng-controller="Product">
    <span>{{getTranslation()}}</span>
</div>

返回promise的异步函数将导致无限的摘要错误。

有关更多信息,请参见