0

I am making attempts to style elements based off the attributes highlighted in my JSON file. I am currently using ng-styleto call properties within the JSON objects

ng-style="{color: colors.color }"

The example above doesn't seem to be doing in regards to targeting styling the nodes on my DOM

Does any know how to resolve this issue.

{
 "colors": [
 {
"color": "red",
"value": "#f00",
"message": "Simple message"
 },
 {
"color": "green",
"value": "#0f0",
"message": "Message with <strong>HTML</strong> tags"
 },
 {
"color": "blue",
"value": "#00f",
"message": "Am I going to work? I should not! <script>alert('Hello!');</script>"
}]
}

HTML

<ul class="block-elements">
  <li ng-repeat="details in colors">
   <button ng-click="popupBtn()" ng-style="{color: colors.color }">Click Me</button>
  </li>
</ul>

CONTROLLER

"use strict";

var app = angular.module('nApp');

app.service("dataService", function ($http, $q){
var deferred = $q.defer();
$http.get('app/data/data.json').then(function (response){
    deferred.resolve(response.data);
 });

  this.getcolors = function () {
    return deferred.promise;
};
 })

  angular
  .module('nApp')
   .controller('dataCtrl', ['$scope', 'dataService', '$location', function($scope,    dataService, $location) {

 var promise = dataService.getcolors();
  promise.then(function (data){
  $scope.colors = data.colors;
  });
3
  • Hello 1. Do you read the file in a controller? 2. Use ng-style, no need for ng-attr-style Commented Jul 3, 2016 at 20:12
  • 1
    it should be {{details.value} instead of {{colors.value}} Commented Jul 3, 2016 at 20:17
  • @RamTobolski i used ng-style in the first and it still doesn't work Commented Jul 3, 2016 at 20:23

1 Answer 1

1

Here's is a snippet working:

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

app.controller('mainCtrl', function($scope) {
  $scope.colors = [  
     {  
        "color":"red",
        "value":"#f00",
        "message":"Simple message"
     },
     {  
        "color":"green",
        "value":"#0f0",
        "message":"Message with <strong>HTML</strong> tags"
     },
     {  
        "color":"blue",
        "value":"#00f",
        "message":"Am I going to work? I should not!"
     }
  ]
});
<!DOCTYPE html>
<html ng-app="app">

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.min.js"></script>
</head>
<body ng-controller="mainCtrl">
  <ul class="block-elements">
    <li ng-repeat="details in colors">
      <button ng-click="popupBtn()" ng-style="{ color: details.color }">Click Me</button>
    </li>
  </ul>
</body>
</html>

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

4 Comments

How do i add multiple styles ng-style ??
To add multiple styles, you can either put "inline" (just using a comma to separate them) in tag or create an object. Ex (inline): <button ng-click="popupBtn()" ng-style="{ 'color': details.color, 'margin-left': '35px' }">Click Me</button>.
are the rules slightly different if i am using styles from the JSON file?'color': details.color, 'background-color': details.value
What kind of styles are you using?

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.