2

I'm trying to add border through ng-style but not able to find out how can I concate value from scope variable.

Below all are not working.

<div ng-style="{'border-top' :'1px solid' + myClass}">Demo text</div>
<div ng-style="{'border-top' :'1px solid myClass'}">Demo text</div>
<div ng-style="{'border-top' :1px solid myClass}">Demo text</div>

See Fiddle

1
  • 1
    <div ng-style="{'border-top' :'1px solid '+myClass}">Demo text</div> Commented Apr 15, 2016 at 7:22

2 Answers 2

4

You are missing the space(" ") in expression.

function TodoCtrl($scope) {
  $scope.myClass = "red";
}
* {
  padding: 0;
  margin: 0;
}
.red {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
<div ng-app>
  <div ng-controller="TodoCtrl">
    <div ng-style="{'border-top' :'5px solid '+myClass}">Demo text</div>
    <!--====================================^^^ -->
  </div>
</div>

Fiddle Demo

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

Comments

0

you sould use ng-class directive to add class!

  <div ng-style="{'border-top' :'1px solid' }" ng-class="myClass">Demo text</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.