我试图获取$state
的URL。
基本上我想要做反向的:
$state.href('user.home.view', {}, {absolute: true});
而是获取Abs URL
我想获得该州的名字;
我想通过一个Abs URL到一个函数,然后在angular的项目中获得一个有效的$state
。
我试图获取$state
的URL。
基本上我想要做反向的:
$state.href('user.home.view', {}, {absolute: true});
而是获取Abs URL
我想获得该州的名字;
我想通过一个Abs URL到一个函数,然后在angular的项目中获得一个有效的$state
。
有一个名为$uiView
的数据字段附加到ui-view
元素,它包含视图名称和关联的状态。你可以得到这样的状态:
elem.closest('[ui-view]').data('$uiView').state
甚至
elem.inheritedData('$uiView').state
所以,在你的控制器:
.controller('State1Ctrl', function ($state) {
console.log(elem.closest('[ui-view]').data('$uiView').state); // state1
console.log($state.current.name) ;//will give the state name as well.
});
UPDATE:
Your issue:
https://github.com/angular-ui/ui-router/issues/1651
解决方法:
ANGULAR-UI-ROUTER: Resolve state from URL
您可以通过使用.decorator钩上$stateProvider
暴露内部状态的实现。你可以装饰国家建设者的任何财产;有人选择'parent'
任意。
app.config(function($stateProvider) {
$stateProvider.decorator('parent', function (internalStateObj, parentFn) {
// This fn is called by StateBuilder each time a state is registered
// The first arg is the internal state. Capture it and add an accessor to public state object.
internalStateObj.self.$$state = function() { return internalStateObj; };
// pass through to default .parent() function
return parentFn(internalStateObj);
});
});
现在你可以使用。$$ state()方法来访问内部状态对象。
var publicState = $state.get("foo");
var privateInternalState = publicState.$$state();
//Second, loop over each state in $state.get() and test them against your URL fragment.
angular.forEach($state.get(), function(state) {
var privatePortion = state.$$state();
var match = privatePortion.url.exec(url, queryParams);
if (match) console.log("Matched state: " + state.name + " and parameters: " + match);
});
刚刚更新了这个问题......我想**通过**一个URL在应用程序中获得一个有效的$状态......这可能吗? –
我不知道,因为文档没有提到你现在所期望的。我们正在谈论可能会或可能不会解决一个国家的absURL。虽然可能有一些黑客入侵。 –
谢谢!这是一个很好的解决方法! –
有疑问时...'的console.log($状态)',并检查那里有什么 – charlietfl
检查更新的答案,如果可以帮助你一些。不完全是你想要的,但可以满足你的需求。 –