0

I have the following code, when I type an input into the input box, an output is displayed. I want to be able to color the output text and at the same time keep it on the same line.

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<div ng-app="">
  Input some text here....<input value="abcd" type="text" ng-model="did" placeholder="Enter name here" ng-init="did='Lorem Ipsum'">
  <br><br> I want to color the following text : <br><br> {{did}}
</div>

So then I tried

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">
<div ng-app="">
Input some text here....<input value="abcd" type="text" ng-model="did" placeholder="Enter name here" ng-init="did='only this'">
<br><br>
I want to color <div ng-style="myObj"> {{did}} </div> and this text should be on the same line.
</div>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
  $scope.myObj = {
    "color" : "blue",
  }
});
</script>
</body>
</html>

I accomplished the coloring of the text, but I want it all to be on the same line, kindly advise if I can get this fixed or if I need a different coloring technique.

shubham agrawal
  • 3,435
  • 6
  • 20
  • 31
DannyBoi
  • 636
  • 1
  • 7
  • 23

2 Answers2

3

If you change 'div' to 'span' they will be on the same line. This is because of, span is in-line element, div is block-line element.

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">
<div ng-app="">
Input some text here....<input value="abcd" type="text" ng-model="did" placeholder="Enter name here" ng-init="did='only this'">
<br><br>
I want to color <span ng-style="myObj"> {{did}} </span> and this text should be on the same line.
</div>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
  $scope.myObj = {
    "color" : "blue",
  }
});
</script>
</body>
</html>
Emre
  • 337
  • 1
  • 3
  • 18
1

Just add display:inline style attribute.

$scope.myObj = {
    "color": "blue",
    "display": "inline"
}

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<body ng-app="myApp" ng-controller="myCtrl">
    <div ng-app="">
        Input some text here....
        <input value="abcd" type="text" ng-model="did" placeholder="Enter name here" ng-init="did='only this'">
        <br>
        <br> I want to color
        <div ng-style="myObj"> {{did}} </div> and this text should be on the same line.
    </div>
    <script>
        var app = angular.module("myApp", []);
        app.controller("myCtrl", function($scope) {
            $scope.myObj = {
                "color": "blue",
                "display": "inline"
            }
        });
    </script>
</body>

</html>
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
  • If you see previous answer also solves the problem using instead of div, any idea which method is better practice, I mean any disadvantages or advantages, – DannyBoi Mar 03 '17 at 12:45
  • It's better to use span, instead of inline divs generally, especially for html validation. Of course there may be extraordinary situations that you may want to use div. For reference check answers at: http://stackoverflow.com/questions/1611065/span-vs-div-inline-block – Emre Mar 03 '17 at 12:55
  • 1
    @DannyBoi if your element is `inline` or `inline-block` use a ``. If it's a block level element, use a `
    `. Obviously if you can change the element type, then switch to ``. If you can't, then changing css might help. So I would suggest to go with @Emre 's answer
    – Raman Sahasi Mar 03 '17 at 13:12