如何在 $translateReady 中调用 $translateChangeSuccess 函数?
How to call $translateChangeSuccess function inside $translateReady?
有时在加载页面时,translateChangeSuccess
不会被触发并抛出错误。
因此,只要 $translate 准备就绪,我就需要调用 $translateChangeSuccess
。怎样做才是正确的?
这不是 working.It 没有进入“$translateReady”函数。
$rootScope.$on('$translateReady', function () {
console.log("It enterd Translate ready"+$translate.isReady());
$rootScope.$on('$translateChangeSuccess', myfunction());
});
但是如果我使用这个函数,它就会进入函数内部
$translate.onReady().then(function() {
console.log("It enterd Translate ready"+$translate.isReady());
$rootScope.$on('$translateChangeSuccess', myfunction());
});
我需要在它就绪时调用 $translateChangeSuccess
。我能怎么做 ?如果有人知道请做 help.Thanks!
您可以在根范围内广播 if 并在您想要的任何范围内收听。此外,您不必在前一个事件处理程序中调用它,实际上您正在做的是在 $translateReady
触发时添加一个事件侦听器(又名 $translateChangeSuccess
)。
总之,考虑这样使用:
$translate.onReady().then(function() {
console.log("It enterd Translate ready"+$translate.isReady());
$rootScope.$broadcast('$translateChangeSuccess');
});
$rootScope.$on('$translateChangeSuccess', myfunction);
function myfunction(){
console.log("myfunction called");
}
有时在加载页面时,translateChangeSuccess
不会被触发并抛出错误。
因此,只要 $translate 准备就绪,我就需要调用 $translateChangeSuccess
。怎样做才是正确的?
这不是 working.It 没有进入“$translateReady”函数。
$rootScope.$on('$translateReady', function () {
console.log("It enterd Translate ready"+$translate.isReady());
$rootScope.$on('$translateChangeSuccess', myfunction());
});
但是如果我使用这个函数,它就会进入函数内部
$translate.onReady().then(function() {
console.log("It enterd Translate ready"+$translate.isReady());
$rootScope.$on('$translateChangeSuccess', myfunction());
});
我需要在它就绪时调用 $translateChangeSuccess
。我能怎么做 ?如果有人知道请做 help.Thanks!
您可以在根范围内广播 if 并在您想要的任何范围内收听。此外,您不必在前一个事件处理程序中调用它,实际上您正在做的是在 $translateReady
触发时添加一个事件侦听器(又名 $translateChangeSuccess
)。
总之,考虑这样使用:
$translate.onReady().then(function() {
console.log("It enterd Translate ready"+$translate.isReady());
$rootScope.$broadcast('$translateChangeSuccess');
});
$rootScope.$on('$translateChangeSuccess', myfunction);
function myfunction(){
console.log("myfunction called");
}