使用指令作为模板路由

时间:2016-07-22 20:07:27

标签: angularjs angularjs-ng-route

如何将字段变量传递给ng路由配置中的指令,或者在另一个阶段?

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

如何在路由阶段将指令分配给指令?

1 个答案:

答案 0 :(得分: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" };

}]);