AngularJS UI Router语言 - $state.current.name 在使用 templateURL 时为



我正在使用 Angular 的 ui 路由器并试图获取当前状态的名称。

经过 3 天的工作,我发现当我使用 templateURL 时它是空的。将其更改为模板:"测试"后,状态名称神奇地出现...

有可能让它工作吗?这是演示

Code on above link

这里需要$watch,因为它都是异步的,我们不能依赖时间:

myApp.controller('navCtrl', ['$state', '$scope','$timeout', function($state, $scope, $timeout){
    $scope.currState = $state;
    console.log("This one have some objects: ");
    console.log('by reference:', $scope.currState);
    console.log('by value:', JSON.parse(JSON.stringify($scope.currState)));
    // console.log("But when I want to access name its empty: ");
    // $timeout(function() {
    //    console.log($state.current.name);
    // });
    // use this instead:
    $scope.$watch('currState.current.name', function(newValue, oldValue) {
      console.log(newValue);
    });  
}]);

console.log('by reference:', $scope.currState);确实有效,因为它使用了对对象的引用。当我们在控制台中看到它时,对象已经更改。

与上述 console.log('by value:', JSON.parse(JSON.stringify($scope.currState))); 的输出进行比较,后者在执行点冻结输出。你会发现一个空的$state.current.name

另请参阅:如何更改控制台.log的默认行为?(*Safari 中的错误控制台,无附加组件*)

相关内容

最新更新