如何在AngularJS中的异步操作后推迟角度表达式评估?

时间:2017-11-22 06:56:16

标签: javascript angularjs asynchronous

如何在异步操作后推迟HTML中角度表达式的评估?

我需要只在AJAX调用后定义的translate函数。

以下是代码:

angular.module('app', [])
  .run(['$rootScope', '$http', function($rootScope, $http) {
    $rootScope.locale = 'en';
    $http.get('/resources').then(response => {
      let dictionary = response.data;
      $rootScope.tr = (key, ...args) => {
        let text = dictionary[$rootScope.locale][key];
        args.forEach((value, index) => text = text.replace(`{${index}}`, value));
        return text;
      };
    });
  }

在HTML中,

<body ng-app="app"> {{tr('page.title')}} </body>

0 个答案:

没有答案