基于$ routeProvider params的动态ng-controller指令

时间:2017-01-21 16:51:21

标签: angularjs ngroute

我正在开发一个应用,我希望多次重复使用单个视图模板,并在不同的网页/路由上略有不同。

例如,如果我有类似“玩具”和“女装”的类别,每个页面将具有相同的布局,但会有不同的过滤选项等。

我认为最好的方法是在模板中使用动态ng-controller指令。

我无法弄清楚如何将类别从路由提供者传递到视图以使ng-controller动态...

app.config(['$routeProvider', function($routeProvider) {
    $routeProvider.
    when('/products/:category', {
        templateUrl: '/views/products.html',
        controller: 'productsController'
        // pass :category param to productsController
    });
}]);

app.controller('productsController', ['$scope', function($scope) {
    // get the parameter
    // do something to set the proper filter section from the parameter:
    // i.e women's clothing would contain filter by size, material, type, etc, toys would not
}]);

或者我接近这一切都错了?我不想重新编写每个视图的代码,因为它们会有很小的变化。

1 个答案:

答案 0 :(得分:2)

如果使用默认ngRouter,则注入import,如果使用ui-router,则注入$routeParams。类别将在注入的对象中可用

相关问题