2017-05-19 42 views
1

AngularJS 1.5。
如何使用ui-sref指令傳遞組件內的參數?angularjs 1.5使用ui-sref將參數傳遞給組件使用ui路由器和組件,使用ui-sref

了header.html:

<a ui-sref="dashboard({contentType: 'company'})">Company</a> 

app.js:

.state('dashboard', { 
     url: "/dashboard", 
     component: "dashboard", 
     resolve: { 
     } 
    }) 

儀表板component.js:

angular.module('myModule').component('dashboard', { 
    templateUrl: '/views/admin/dashboard.html', 
    controller: DashboardController, 
    binding: { 
     contentType: '<' // This is not working... 
    } 
}); 

dashboard.html:(希望看到的contentType在這裏 - 但是沒有...)

contentType: {{$ctrl.contentType}}

回答

2

你需要告訴你的國家,它可以與一些PARAMS調用。有兩種方法可以得到參數。這裏有一個簡單的解釋:

  1. PARAMS這是不是URL的一部分

    .state('dashboard', { 
        url: "/dashboard", 
        params: {contentType: null}, 
        component: "dashboard", 
        resolve: { 
        ... 
        } 
    }) 
    

編輯 URL

.state('dashboard', { 
    url: "/dashboard/:contentType", 
    component: "dashboard", 
    resolve: { 
    ... 
    } 
}) 
  • PARAMS的一部分:如果你看到在我提供的評論中,他們並沒有直接嘗試使用控制器中的參數值,但他們正在使用它獲得某些東西resolve d。現在

    ,來到你的問題的解決方案,

    的一種方法是,你可以在控制器注入$stateParams和使用$stateParams.tester

    或者,如果你願意,你也可以使用resolve獲得直接傳遞的值,如:

    $stateProvider.state('userlist', { 
        url: '/users', 
        params: { 
         tester: null 
        }, 
        component: 'users', 
        resolve: { 
         tester: function($stateParams) { 
         return $stateParams.tester; 
         } 
        } 
        }); 
    

    second example plunker

  • +0

    謝謝,不知道關於「PARAMS」但儘管如此,這並沒有奏效爲了我。 1.在儀表板的綁定中,我還需要「contentType:'<'」嗎? 2.儀表板組件被放置在ui-view指令中 - 我需要指定它嗎(類似於

    ? – chenop

    +0

    @chenop這是一個[示例plunker](https://plnkr.co/edit/CgHJ7HSeFOu6dyGjv0QW?p=preview)演示它是如何與'ui-router'和'components'一起工作的,如果這有助於你解決問題,那麼很好,否則你可以用我的代碼,叉子和共享URL來修改它 – tanmay

    +0

    欣賞你的幫助意願。闖入者證明了這個問題: https://plnkr.co/edit/ebxaP8xcCXeIaLiiZ61K?p=preview – chenop

    1

    添加網址PARAM contentType的狀態

    .state('dashboard', { 
         url: "/dashboard/:contentType", 
         component: "dashboard", 
         resolve: { 
         } 
        }) 
    
    相關問題