2

I want to add multiple style attribute for accordion group using ng-style with conditions as ng-class is not working with accordion.

Here is how i am trying:

ng-style="{ border: ivrMessageForm.ivr_messagetitle.$error.required && !isFormValid ? '2px solid' : 'none' }"

this is working fine. but want to add border-color too.

I tried this:

ng-style="{ border: ivrMessageForm.ivr_messagetitle.$error.required && !isFormValid ? '2px solid' : 'none', border-color: ivrMessageForm.ivr_messagetitle.$error.required && !isFormValid  ? 'red' : 'none'}"

but it gives me parse error.

i also tried this one but same parse error:

ng-style="ivrMessageForm.ivr_messagetitle.$error.required ? {border:'3px solid', border-color: 'red'} : {border:'none', border-color: 'none'}"

can anyone help me how to add multiple style attributes with multiple conditions using ng-style.

1

2 Answers 2

7

You should add single quotes to 'border-color'

ng-style="{ border: ivrMessageForm.ivr_messagetitle.$error.required && !isFormValid ? '2px solid' : 'none', 'border-color': ivrMessageForm.ivr_messagetitle.$error.required && !isFormValid  ? 'red' : 'none'}"
Sign up to request clarification or add additional context in comments.

4 Comments

single quote is necessary for all next attributes?
what about if i want to add same style on $error.pattern?
Depends on the attribute name. This is not a angularjs knowledge but javascript. If the property name contains more than one word, you must write it in hump format or or use quotation marks.That means you can also change 'border-color' to borderColor.
can you optimize this. Let suppose define style in js file as $scope.css = { border: '3px solid', 'border-color': 'red' } How can i pass this css variable in ng-style with condition?
1

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
	$scope.styleobj = {};
  $scope.borderflag = true;
  $scope.widthflag = true;
  $scope.getStyle= function(){  
     if($scope.borderflag){
				$scope.styleobj.border = '2px solid';
     }if($scope.widthflag){
     		$scope.styleobj.width = '100%';
        $scope.styleobj.font = 'italic bold 12px/30px Georgia, serif';
     }
     console.log($scope.styleobj);
     return $scope.styleobj;
  }
	
    $scope.name = 'Arindam Banerjee';
    
    
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>

<div ng-app="myApp" ng-controller="MyCtrl">
  Hello, {{name}}!
  <p ng-style="getStyle()">
   Hello, {{name}}!
  </p>
</div>

##

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.