admin管理员组文章数量:1393059
I am trying to dynamically update the page title.
Consider a state defined thus:
$stateProvider.state('login', {
url: '/login',
templateUrl: '/templates/views/login.html',
controller: 'AuthCtrl',
data: {title: 'Log in'}
}
In the page HEAD section:
<title page-title></title>
According to the documentation, I am supposed to be able to access my custom data property:
app.directive("pageTitle", function ($state) {
return {
restrict: 'A',
template: "{{title}}",
scope: {},
link: function (scope, elem, attr) {
scope.title=$state.current.data.title; //wrap this in $watch
console.log('page state',$state.current.data.title);
}
}
});
But this returns undefined
. Any ideas? Thanks!
I am trying to dynamically update the page title.
Consider a state defined thus:
$stateProvider.state('login', {
url: '/login',
templateUrl: '/templates/views/login.html',
controller: 'AuthCtrl',
data: {title: 'Log in'}
}
In the page HEAD section:
<title page-title></title>
According to the documentation, I am supposed to be able to access my custom data property:
app.directive("pageTitle", function ($state) {
return {
restrict: 'A',
template: "{{title}}",
scope: {},
link: function (scope, elem, attr) {
scope.title=$state.current.data.title; //wrap this in $watch
console.log('page state',$state.current.data.title);
}
}
});
But this returns undefined
. Any ideas? Thanks!
1 Answer
Reset to default 7The variable is indeed available to the page, but in your directive, you have created an isolated scope. whenever you use the scope: {}
option on a directive, it creates a scope which is limited to the directive only.
You have two options which will help to solve this issue.
- You can create the directive without the
scope:{}
option, which would allow the directive full access to thescope
which exists on the parent page. The drawback here is that this limits the use of the directive to a single instance per scope. - Create a binding in the scope option and pass the variable from the HTML to the directive
Directive: scope: {title: '=title'}
HTML: <title><page-title title="{{$state.current.data.title}}"></page-title></title>
本文标签: javascriptCan39t access statecurrentdata in angular uirouter as per documentationStack Overflow
版权声明:本文标题:javascript - Can't access $state.current.data in angular ui-router as per documentation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744769170a2624230.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论