最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Access AngularJS constant in a view - Stack Overflow

programmeradmin3浏览0评论

I'm gonna try to describe the scenario, bear with me please.

I have a Angular constant called Urls filled with the routes and some methods to access them.

app = angular.module "app"

app.constant "Urls",
    routes: 
        # Main stuff
        overview: "/"
        users: "/users"
        user: "/users/:id"

    overview: ->
        return @.routes.overview
    users: ->
        return @.routes.users
    user: (id)->
        return @.routes.user
                    .replace(":id", id)

The reason for using a constant for this is that I need to access it during the config phase of our application as well as use it in controllers.

What I want to achieve is that I want to use it in a view as well, for instance;

<ul class="foo">
    <li ng-repeat="user in users">
        <a href="{{Urls.user(user.id)}}">
            {{user.name}}
        </a>
    </li>
</ul>

Is there a way to achieve something like this? Preferably without assigning the Urls constant to $rootScope or assigning it to every controllers $scope?

I'm gonna try to describe the scenario, bear with me please.

I have a Angular constant called Urls filled with the routes and some methods to access them.

app = angular.module "app"

app.constant "Urls",
    routes: 
        # Main stuff
        overview: "/"
        users: "/users"
        user: "/users/:id"

    overview: ->
        return @.routes.overview
    users: ->
        return @.routes.users
    user: (id)->
        return @.routes.user
                    .replace(":id", id)

The reason for using a constant for this is that I need to access it during the config phase of our application as well as use it in controllers.

What I want to achieve is that I want to use it in a view as well, for instance;

<ul class="foo">
    <li ng-repeat="user in users">
        <a href="{{Urls.user(user.id)}}">
            {{user.name}}
        </a>
    </li>
</ul>

Is there a way to achieve something like this? Preferably without assigning the Urls constant to $rootScope or assigning it to every controllers $scope?

Share Improve this question edited Apr 3, 2014 at 12:31 Rasmus asked Apr 3, 2014 at 12:11 RasmusRasmus 4,1807 gold badges23 silver badges32 bronze badges 5
  • You need to inject Urls in controller. – Satpal Commented Apr 3, 2014 at 12:14
  • @Satpal, right, what I'm asking is if theres a way to get away from that. Or are you telling me it's actually impossible? – Rasmus Commented Apr 3, 2014 at 12:16
  • 1 I assume that's coffesscript you're using there. You might get more help if you use plain javascript. – jonhobbs Commented Apr 3, 2014 at 12:25
  • 1 I don't understand how that would be relevant whatsoever. I think I've been pretty clear of what I want to achieve. If someone knows the answer I hope they will help me regardless if I'm writing application logic in Coffeescript or plain ol' Javascript – Rasmus Commented Apr 3, 2014 at 12:30
  • The simplest way I know is to use $rootScope, the cleaner way the injection in a controller. You must assign Urls to a scope if you want to use it in a view. – Mickaël Gauvin Commented Apr 3, 2014 at 13:18
Add a ment  | 

1 Answer 1

Reset to default 11

Is not possible. You need to inject Urls in the controller $scope (or parent using $parent) because {{ var }} is interpolated to $scope.var

Does not have any performance problem if you inject Urls in your controller and write $scope.Urls = Urls

Example:

angular.module('myapp').controller('myController', ($scope, Urls) {
  $scope.Urls = Urls;
}
发布评论

评论列表(0)

  1. 暂无评论