Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度1.5组件访问ui路由器$状态_Javascript_Angularjs_Angular Ui Router - Fatal编程技术网

Javascript 角度1.5组件访问ui路由器$状态

Javascript 角度1.5组件访问ui路由器$状态,javascript,angularjs,angular-ui-router,Javascript,Angularjs,Angular Ui Router,使用Angular1.5.x和AngularUI Router 1.0-alpha中引入的新.component()方法,如何从组件内部访问当前路由器状态 例如(我在这里使用ES2015,如下所示): 从“角度”导入角度; 常数头=角度 .module('标题',[])) .component('标题'{ 模板:` {{$state.current.name} ` }); 注意:header是一个子组件,ui路由器是在父组件中注入和配置的 但是,$state.current.name甚至只是$

使用Angular1.5.x和AngularUI Router 1.0-alpha中引入的新
.component()
方法,如何从组件内部访问当前路由器状态

例如(我在这里使用ES2015,如下所示):

从“角度”导入角度;
常数头=角度
.module('标题',[]))
.component('标题'{
模板:`
{{$state.current.name}

` });
注意:header是一个子组件,ui路由器是在父组件中注入和配置的

但是,
$state.current.name
甚至只是
$state
都不会在组件的模板中显示任何内容

我试图做的全局想法是根据应用程序的当前状态在头上设置一个类


我已尝试将状态设置为
$rootScope
,但也不起作用。。。我可能遗漏了一些东西,因为我是Angular的新手,所以可能有人有想法?

您将其注入控制器:

import angular from 'angular';

const header = angular
    .module('header', [])
    .component('header', {
        template: `
            <p>{{$ctrl.$state.current.name}}</p>
        `,
        controller: ['$state', function($state) {
            this.$state = $state;
        }]
    });
从“角度”导入角度;
常数头=角度
.module('标题',[]))
.component('标题'{
模板:`
{{$ctrl.$state.current.name}

`, 控制器:['$state',函数($state){ 这个。$state=$state; }] });

注意模板中的
$ctrl

没错,谢谢!因为我的组件从来没有控制器,所以我没有想过创建一个控制器来传递$state:)
import angular from 'angular';

const header = angular
    .module('header', [])
    .component('header', {
        template: `
            <p>{{$ctrl.$state.current.name}}</p>
        `,
        controller: ['$state', function($state) {
            this.$state = $state;
        }]
    });