如何在异步操作后推迟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>