2016-07-22 81 views
1

如何在ng路由配置中或在另一個階段將字段變量傳遞給指令?指令作爲模板的路線

.when('/test',{template:"<my-directive fields=field></my-directive>"}) 

如何在路由階段將param指定給指令?

+0

你能更具體?像你想要設置的屬性?你能告訴我們你的控制器嗎? –

+0

我需要爲指令分配字段,正如我上面解釋的,我的問題是:從哪裏我將帶領域參數?我應該使用決心返回字段或從指令控制器我應該得到領域? –

+0

通常,您需要準備控制器內的數據(例如'$ scope'),然後可以從模板訪問數據。 –

回答

1

定義模塊依賴時請務必註明您的指令:

var app = angular.module('sampleApp', [ 
    'ngRoute', 
    'myDirective' // here, you need to include your directive module 
]); 

然後,定義你的路由:

app.config(['$routeProvider', function($routeProvider) { 
    $routeProvider 
     .when('/', { template: "This is the default Route" }) 
     .when('/test', { 
      template: '<my-directive fields="field"></my-directive>', 
      controller: 'testController' 
     }) 
     .otherwise({ redirectTo: '/' }); 
}]); 

和控制器:

app.controller('testController', ['$scope', function($scope) { 

    $scope.field = { your: "data here" }; 

}]);