Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

When I want to init a variable on first start up of a controller I do this in my view

<div ng-controller="testCtrl" ng-init="<%some var from server %>"> <div>

However, I'm now using ui-router like this:

 .state('index', {

            url: "/",
            templateUrl: "/register-form.html",
            controller: "testCtrl"
        })

Because I no longer render the <div ng-controller= etc how to I still init and pass my <%some var from server %> from the template? register-form.html does not have the controller tag anymore because ui-router takes care of it.

share|improve this question
    
What is the nature of the variable ? It may have some importance, because it might belong to somewhere else. – axelduch Feb 4 '14 at 20:09
    
@aduch I pass in an ID from server side, its rendered outside AngularJS App so need to somehow be passed in. – Spaceships Feb 4 '14 at 20:14
up vote 2 down vote accepted

Just put it on top of your template:

<div ng-init="<%some var from server %>"> <div>

ngInit doesn't depend on ngController, it just initiates a variable on the scope.

share|improve this answer
    
why was this marked down? would this not work? seems like a good idea. I could then set this into a service making accessible to all controllers? bad idea? – Spaceships Feb 4 '14 at 21:01
    
@Spike Of course it would work. a downvote is only one user's opinion, and it's lame if it's not followed by a comment. – Ilan Frumer Feb 4 '14 at 21:10
1  
@Spike When I write SPA applications, the server side's role is only to serve resources and static assets, and that's also what I recommend as a best practice. In the question you want to inject <%some var from server %> so I showed you how to do so. My general approach to answers is to give scoped answers without trying to refactor the whole architecture unless it's obviously needed. It's not practical to enforce users to think in your way when they already have working applications and only need a little patch. – Ilan Frumer Feb 4 '14 at 21:20
    
If using child views and the ui-router, you can use the ng-init to pass data to the child controller. If for example you use a childview in a ng-repeat, you can pas the current object to the child controller. – Mark Apr 7 '15 at 13:00
.state('index', {
    url: "/:id",
    templateUrl: "/register-form.html",
    resolve : {
       id  : function ($http, $stateparams ) {
         var id = '';  
         // resolve id from somewhere, 
         // call to dom hidden field,service/server injection
         return  id; 
       }
    }
    controller: "testCtrl"
});

and a controller :

 .controller('testCtrl', ['id', function(id) {
        //resolved id from state
    }]);

this is another option. if you can't modify url, but want something similar to ng-init. Take a look into resolve function, which takes parameter name id and trying to resolve it, before actual init of controller

hope it will give your some idea.

share|improve this answer
    
very nice answer thank you. stateparams is not something I can use in my case, but I marked this answer up because in the future this could be a good option. thank you – Spaceships Feb 5 '14 at 9:37
1  
yeah. params from function should be defined as you wish, as they are just dependencies, I just posted from my project. :) – Eugene P. Feb 5 '14 at 9:39

As you want to pass an id, I recommend you to pass through url in $stateParams

.state('index', {
    url: "/:id",
    templateUrl: "/register-form.html",
    controller: "testCtrl"
});

in you controller now

.controller('testCtrl', ['$stateParams', function($stateParams) {
    // cast id to an int since it comes from url now
    var id = +$stateParams.id;
}]);
share|improve this answer
    
thats not an option for me I'm afraid. I was hoping its possible to use init for the controller. – Spaceships Feb 4 '14 at 21:02
    
So yes it is possible! For example if you have a child view that has its own controller, you could use the ng-init to pass data to it: <div class="panel-body" ng-repeat="z in projectToPrint.zones"> <div ui-view="zoneDetails@projectsfull" ng-init="zoneToPrint = z"></div></div> – Mark Apr 7 '15 at 13:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.