Joy
Joy

Reputation: 7028

ui router not activating the controller in template context

I have a template which would be resolved in ui-view as

<div class="row login" >
    <div class="col-xs-12 col-sm-8 col-md-6 " ng-class="offSet">
    </div>
</div>

Now The ui router state which resolves this template from templateUrl is

 .state('login.register', {
    url: "login/register",
    views: {
        "@": {
            templateUrl: "/Account/Register"
        },
        controller: "loginController"
    }
})

And the controller is :

ngApp.controller('loginController', ['$scope', '$state', function ($scope, $state) {
$scope.offSet = "col-lg-offset-3";
$scope.login = function () {
    $state.go('dashboard.home');
   }
}])

Now what I want is the ng-class should get applied as soon as the template resolves and rendered. But thats not happening . the ng-class is not getting applied if I dont provide ng-controller attribute .

However when I put the ng-controller='loginController' in my template as

<div class="row login" ng-controller="loginController">
    <div class="col-xs-12 col-sm-8 col-md-6 " ng-class="offSet">
    </div>
</div>

Then the class gets attached and I can see the effect on resulting dom. But as far as ui router documentation says . the controlller gets attached to the dom as soon as the template is resolved .

Any idea why without ng-controller attribute its not working ????

Upvotes: 1

Views: 336

Answers (1)

Radim K&#246;hler
Radim K&#246;hler

Reputation: 123861

You are almost there, solution here is surprisingly simple. Controller belongs to the view, in our case to "named" view, not to the cluster views:

.state('login.register', {
    url: "login/register",
    views: {
        "@": {
            templateUrl: "/Account/Register"
            controller: "loginController"     // here it will be used
        },
        //controller: "loginController"       // just skipped
    }
})

In case we'd like to reuse one controller for more named views, we just repeat its definition for each of them...

Upvotes: 1

Related Questions