Reputation: 9183
I have the following markup:
<p class="{{ UserMessageStyle }}" > Some Message </p>
And then in Angular Controller When I want to pass a specific class name to the
variable UserMessageStyle
, I do the following:
$scope.UserMessageStyle = [];
if(condition_is_met)
{
$scope.UserMessageStyle.push("alert-danger");
}
But my problem is that, when once the above statement is run, then I cannot change it. It has a reason because I have declared UserMessageStyle
as []
which mean on each instance of push()
a new key&value pair will be added.
My Question is that, how should I use push()
where I don't want my UserMessageStyle
to be an object or an array. I want it to be a simple variable which is overriden on each instance call of push()
. Something like this:
// the declaration of the variable as an object is removed.
if(condition_is_met)
{
$scope.UserMessageStyle.push("alert-danger");
}
But the above statement causes this error:
cannot read the property `push()` of undefined.
What should I do?
Upvotes: 0
Views: 37
Reputation: 17579
First of all you do not need array here, class attribute understand space separated list
$scope.mystyles = "alert-danger alert-danger-bright"
and layout
<p class="{{ UserMessageStyle }}" > Some Message </p>
Also consider using ng-class
directive for that
<p ng-class="UserMessageStyle" > Some Message </p>
Upvotes: 1
Reputation: 1940
It sounds to me that you are looking for a normal variable instead of an array. why dont you do something like this:
if(condition_is_met)
{
$scope.UserMessageStyle = "alert-danger";
}
Upvotes: 2