0

I'm trying to add the class post to my div #wrap when i'm on the page /post. That's what I have:

$routeProvider

when('/post', {
    templateUrl : 'views/post.php',
    controller  : 'postCtrl'
})

Controller

carolvaladares.controller('postCtrl', function($scope) {
    $scope.post = true;
});

HTML

<div id="wrap" ng-class="{true: 'post'}[post]">

When on /post, $scope.post is true. If the $scope.post is true, add the class post to #wrap

The thing is that when I go to /post, It does not read the $scope.post unless I use ng-controller="postCtrl" manually.

Thanks in advance.

-- EDITED --

The $scope.post returns true when I use {{post}} on /post. Still I can't work with ng-class.

-- EDITED --

The problem still happening because the #wrap is out of my ng-view. So I guess what I'm trying to do, the way I'm trying to do won't be possible.

7
  • Does your controller load when you navigate to /post and i hope you are using ng-view Commented Jan 11, 2016 at 15:10
  • Yes. I'm using ng-view, and the controller loads when I navigate to /post. I tested it using a simple $scope.message and {{message}} to confirm it. Commented Jan 11, 2016 at 15:13
  • Did my answer work... do you get any console error message? Commented Jan 11, 2016 at 15:26
  • It is not clear where #wrap is placed in html, please check scopes, may be #wrap is outside controller's scope. Check example Commented Jan 11, 2016 at 15:27
  • @Artem Yeah man, it worked this way here too. But I'm trying to understand how could I affect some element out of my ng-view without specifying the controller manually. Because if I specify the controller, It would affect every page on my application. Commented Jan 11, 2016 at 15:36

5 Answers 5

1

I think the problem is your HTML:

<div id="wrap" ng-class="{ className: post === true }">

should work.

Sign up to request clarification or add additional context in comments.

Comments

0

Try

<div id="wrap" ng-class="{'post': post}">

Also don't forget to add the ng-controller

1 Comment

It works if I use ng-controller="postCtrl" manually on #wrap. That way, all the other pages would also add the class post.
0

ng-class works like this:

ng-class="{'classNameToApply': angularExpression}"

The 'classNameToApply' will be applied when the angularExpression evaluates to something truthy.

So in your case, to apply the class of 'post', you would do <div id="wrap" ng-class="{'post': post}">

1 Comment

It works if I use ng-controller="postCtrl" manually on #wrap. That way, all the other pages would also add the class post.
0
<div id="wrap" ng-class="{true: 'post'}[post]">

The statement you have given for ng-class should work, could you change your post variable to something else to check if its working like below:

<div id="wrap" ng-class="{true: 'post'}[someValue]">

Apart from this, there is other solutions below which you can try:

Sol1:

<div id="wrap" ng-class="{'post': post}">

Sol2

<div id="wrap" ng-class="post ? 'post' : 'somethingelse' }">

Comments

0

I would suggest you try something like this:

when('/post', {
    templateUrl : 'views/post.php',
    controller  : 'postCtrl',
    activetab: 'post'
})

and add:

<div id="wrap" ng-class="{post: $route.current.activetab == 'post'}"">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.