2

What I want is if <p>Green.</p> then it has style of style="color: green". If <p>Red.</p> then it has style of style="color: red".

What I have is

<p
    ng-model="please_take_my_inner_html"
    ng-style="{ color: please_take_my_inner_html == 'Green.' ? 'green' : 'red' }"
>
   Green.
</p>

This is not working. But I hope you know what I want to achieve.

notalentgeek
  • 4,939
  • 11
  • 34
  • 53
  • Why not set classes and change p class with ng-if? – fedesc Aug 24 '16 at 11:00
  • 1
    @FedSc no need to use `ng-if` for this, but `ng-class` or `ng-style` would be the way to go... OP, looks like you got pretty close, I'm not sure what you're trying to make different without some more code. Try using interpolation as the text itself and it'll save you work/trouble. Also ng-model is useless on non-input fields – casraf Aug 24 '16 at 11:03
  • Where does `Green`inside the `

    ` comes from? Is it hardcoded?

    – T J Aug 24 '16 at 11:57
  • while it is clear what you are trying to accomplish, it's not really clear *why* you would want to do this. storing HTML in angular is an ant-pattern, and trying to scan arbitrary HTML content to use in application logic is counter-intuitive and error prone. – Claies Aug 24 '16 at 12:36
  • @casraf, I want to make my own chat box with Angular and other things. I have managed to set the color of "Available" to green when the user is online and turn it red when the user is not online, using SocketIO. However, the initial font color is neither red nor green because in order to change color, client need to receives event from server. So I use AngularJS `ng-repeat` to populate the online list and want to change the font color to green for any user online. In the end I want, if the inner HTML of

    is "Available." then turn it green, otherwise please make it red.
    – notalentgeek Aug 24 '16 at 23:57
  • @T J, it is from `ng-repeat` that is from users collection from MongoDB. If the user is online then the inner HTML of `

    ` should be green colored.
    – notalentgeek Aug 25 '16 at 00:01
  • @Claies, I am new to this AngularJS stuffs, would like to know what is the best practice if you could point me to something (link, article, etc). I just want to make a friend list for chat box. – notalentgeek Aug 25 '16 at 00:02

2 Answers2

1

I don't understand your use-case but it would be possible with a custom directive that's checking the html text.

But as mentioned in the comments I also wouldn't do it like this. Instead use a model for your data and your class / style and use it in your html markup.

Something like in the demo below should work or in this fiddle.

(As mentioned in the other answer ng-model can't be used here. See the docs for ngModel for possible elements.)

angular.module('demoApp', [])
 .controller('mainController', MainController)
 .directive('innerStyle',innerStyle)
  
function innerStyle() {

 return {
   link: function(scope, element, attrs) {
    //console.log(element.text())
     var style = element
       .text().trim().toLowerCase().replace('.', '');
     element.addClass(style);
    /*
     // switch also possible
     switch(element.text().trim()) {
       case 'Green.':
         console.log('grenn style');
          element.addClass('green');
          break;
        case 'Red.':
         console.log('red style');
          element.addClass('red');
          break;
      }*/
    }
  }
}

function MainController() {
 var vm = this;
  
  vm.data = {
   firstModel: {
     text: 'Green.',
      class: 'green'
    },
    secondModel: {
     text: 'Red.',
      class: 'red'
    }
  };
}
.green {
  color: green;
}

.red {
   color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="demoApp" ng-controller="mainController as ctrl">

  <!-- with a directive that scans inner-html -->
  <h2>inner text with directive</h2>
  <p inner-style>
  Green.
  </p>
   <p inner-style>
    Red.
  </p>
  
  <!--I wouldn't do  the above, instead use a model. Something like this <br/>-->
  <h2>same with a model</h2>
  <p ng-class="ctrl.data.firstModel.class">
    {{ctrl.data.firstModel.text}}
  </p>
  <p ng-class="ctrl.data.secondModel.class">
    {{ctrl.data.secondModel.text}}
  </p>
</div>
AWolf
  • 8,770
  • 5
  • 33
  • 39
  • I use the innerStyle with the `switch(){}` statement. However, `console.log(element.text().trim());` returns `{{_Model_Admin.Admin_Bool_Available}}.` as I filled the inner HTML from `ng-repeat` populate from a MongoDB collection. Any clue? – notalentgeek Aug 25 '16 at 00:24
  • Perhaps this directive would works as I want if the `ng-repeat` load the database first then execute `innerFunction()`? – notalentgeek Aug 25 '16 at 00:29
  • I got it working with [http://stackoverflow.com/questions/25536514/run-directive-after-ng-repeat](http://stackoverflow.com/questions/25536514/run-directive-after-ng-repeat) the answer from Kousha. It says it is not the optimal answer, but I guess this does matter for me as it is now. – notalentgeek Aug 25 '16 at 01:22
0

We can not use ng-model with p element or any other element that doesn not accepts input from user. It will bind value of only input,select and textarea element. In short it will work with form elements.

In input element we can consider elements mentioned below.

 - input 
 - text
 - checkbox
 - radio
 - number
 - email
 - url
 - date
 - datetime
 - local
 - time
 - month
 - week
Manish
  • 4,692
  • 3
  • 29
  • 41