如何在$ routeProvider中将变量传递给$ scope

Ant*_*n I 5 angularjs angularjs-scope

我需要在$ routeProvider中将变量传递给$ scope.代码如下:

.when('/:zone/edit/:id', {
      templateUrl: 'views/edit.html',
      controller: 'EditSet'
    })
    .when('/articles/edit/:id', {
      templateUrl: 'views/edit-article.html',
      controller: 'EditSet',
      params: {zone: 'articles'}
    })
Run Code Online (Sandbox Code Playgroud)

为了得到我使用的参数$scope.params.zone,它在第一种情况下起作用(:zone),在第二种情况下不起作用.

在这种情况下我该怎么办?

Dea*_*ard 4

所以我认为你最好使用函数支持来templateUrl实现这一点:

.when('/:zone/edit/:id', {
    templateUrl: function(params) {
        if (params.zone == "article") {
            return 'views/edit-article.html';
        }

        return 'views/edit.html';   
    },
   controller: 'EditSet'
})
Run Code Online (Sandbox Code Playgroud)