0

message.viewed is a boolean value.I need to change attribute of my message content according to the boolean value.Here I used one method.I did not work for me.

<div  ng-style="message.viewed ? {'font-weight': 'bold''} : {'font-weight': 100}" >
<span >{{message.Title}}</span> {{message.details }}
</div>
1
  • Any error in the console? And there are two single quotes after bold, I guess it's a typo in the question? Commented Mar 27, 2017 at 9:18

6 Answers 6

3

You should change your ng-style to this

<div  ng-style="{'font-weight' : message.viewed ? 'bold' : '100'}" >
    <span >{{message.Title}}</span> {{message.details }}
</div>
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you problem was fixed.
@HRCJ No problem. Don't forget to mark the answer that helped you
I tried to mark.But I have to wait 10 mins.I'm waiting.
1

change the ternary condition inside ng-style like this

 <div  ng-style="{'font-weight': message.viewed ? 'bold' : '100'}" >
<span >{{message.Title}}</span> {{message.details }}
</div>

Demo

angular.module("app",[])
.controller("ctrl",function($scope){

$scope.message = {"viewed":true,"Title":"sample"}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
 <div  ng-style="{'font-weight': message.viewed ? 'bold' : '100'}" >
<span >{{message.Title}}</span> {{message.details }}
</div>
</div>

2 Comments

Thank you problem was fixed.
@HRCJ no prob bro :D
1

Or you can use [style.fontWeight] instead of ng-style if its a matter of a single property.

<div  [style.fontWeight]="message.viewed ? 'bold' : '100'" >
    <span >{{message.Title}}</span> {{message.details }}
</div>

2 Comments

Is this AngularJS v1 valid?
Thank you problem was fixed.
1

It would work fine if there is no two single quotes after bold

<div ng-style="message.viewed ? {'font-weight': 'bold'} : {'font-weight': 100}">
<span>{{message.Title}}</span> {{message.details }}

3 Comments

Thank you problem was fixed.
unnecessary use of same property font-weight twice
Yes I know. I was just pointing out syntax error. We can set new style in else part also.
1

Best way to assign a $scope variable to ng-style

$scope.style = message.viewed ? '{'font-weight': 'bold'}' : '{'font-weight': '100'}'

Comments

0

assume message.viewed expression return true/false so use

 ng-style="{font-weight:{ true:'bold', false: 100} [ message.viewed ] }"

4 Comments

Is this AngularJS v1 valid?
Yes. absolutely
Interesting, first time I see this notation. Thanks for sharing ;)
this is helpful when we can apply multiple conditions and classes based on different condition likewise [ {true:'whetever', flase:'whatelse'}[expression1],{true:'applyifTrue'}[expression2] ]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.