0

I'm still in process learning Angular.

For my current project I need to display HTML (eg: <h1>note1</h1><span>my first note</span><br><h1>note2</h1><span>my second note</span>) in my view. I know everyone will suggest to use ng-bind-html, but that is not what I want.

What I want is, i want to do checking if string HTML have note2 then display my second note.

In controller I know how to find string note2 which is use .match.

JSON

items =
[
    {
        "description": "<h1>note1</h1><span>my first note</span><br><h1>note2</h1><span>my second note</span>"
    }
]

In controller find String note2

angular.forEach(items, function(value, key) {
    if (value.description.match("note2")) {
        console.log(value.description);
        // will output HTML <h1>note1</h1><span>my...
    }
    else {
        console.log("string not found");
    }
});

I can do till find String note2, but to display my second note I don't have any idea.

Can someone help me with this, thanks..

2
  • Can you change items.description to value.description? Commented Jan 11, 2017 at 5:17
  • @RameshRajendran yeah already change it Commented Jan 11, 2017 at 7:50

3 Answers 3

1

I haven't test this, but maybe you can use split function

angular.forEach(items, function(value, key){

    if(items.description.match("note2")){
        console.log(items.description);
        var noteValues = items.description.split("<h1>note2</h1>");
        $scope.mySecondNote = noteValues[1]; // value is <span>my second note</span>
    }else{
        console.log("string not found");
    }
});

EDIT

Plunker DEMO here

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

2 Comments

@RameshRajendran OP wants to get ONLY the my second note text. This code get exactly like what OP wants. See DEMO here link
@rachmatsasongko thanks mate! your answer is clean (y)
1

It should be value.description., because items is an array

 angular.forEach(items, function(value, key) {
      if (value.description.match("note2")) {
}

DEMO

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

app.controller("dobController", ["$scope",
  function($scope) {
    items = [{
      "description": "<h1>note1</h1><span>my first note</span><br><h1>note2</h1><span>my second note</span>"
    }];
    angular.forEach(items, function(value, key) {
      if (value.description.match("note2")) {
        console.log(value.description);
        // will output HTML <h1>note1</h1><span>my...
      } else {
        console.log("string not found");
      }
    });
  }
]);
<!DOCTYPE html>
<html ng-app="todoApp">

<head>
  <title>To Do List</title>
  <link href="skeleton.css" rel="stylesheet" />
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
  <script src="MainViewController.js"></script>
</head>


<body ng-controller="dobController">
</body>

</html>

3 Comments

@Sajeetharan. I dont want to output all HTML. I want to get text my second note. What do you do is displaying all HTML text
Plus, If you noticed from my point, I want to display my second note if checking in controller found note2. Have any idea to get this my second note text ?
Those people up vote for this answer is not understanding my question
0

Change to

if (items[0].description.match("note2")) {
    console.log(items[0].description);
    // will output HTML <h1>note1</h1><span>my...
}
else {
    console.log("string not found");
}

2 Comments

ask to OP for, Why OP using loop?
cuz my real data was loop. That is why I'm giving example JASON in loop

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.