gpt4 book ai didi

AngularJS UI 路由器 - 使用 templateUrl 时 $state.current.name 为空

转载 作者:行者123 更新时间:2023-12-04 10:49:48 25 4
gpt4 key购买 nike

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

经过 3 天的工作,我发现当我使用 templateUrl 时它是空的。
改成template:'test'后,状态名神奇地出现了...

有没有可能让它发挥作用?
Here is the demo

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 .

另见: How can I change the default behavior of console.log? (*Error console in safari, no add-on*)

关于AngularJS UI 路由器 - 使用 templateUrl 时 $state.current.name 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29940616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com