【问题标题】:AngularJS display specific String in HTMLAngularJS 在 HTML 中显示特定的字符串
【发布时间】:2017-01-11 04:53:24
【问题描述】:

我仍在学习 Angular。

对于我当前的项目,我需要在我的视图中显示 HTML(例如:<h1>note1</h1><span>my first note</span><br><h1>note2</h1><span>my second note</span>)。我知道每个人都会建议使用ng-bind-html,但这不是我想要的。

我想要的是,我想检查字符串 HTML 是否有 note2 然后显示 my second note

在控制器中,我知道如何找到字符串note2,它使用.match

JSON

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

在控制器中找到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");
    }
});

我可以做到直到找到Stringnote2,但要显示my second note我不知道。

有人可以帮我解决这个问题吗? 谢谢..

【问题讨论】:

  • 你能把items.description改成value.description吗?
  • @RameshRajendran 是的,已经改了

标签: html angularjs arrays string


【解决方案1】:

我没有测试过,但也许你可以使用拆分功能

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");
    }
});

编辑

Plunker DEMO here

【讨论】:

  • @RameshRajendran OP 只想获取 my second note 文本。这段代码完全符合 OP 的要求。在此处查看演示link
  • @rachmatsasongko 谢谢老兄!你的答案是干净的(y)
【解决方案2】:

应该是value.description.,因为items是一个数组

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

演示

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>

【讨论】:

  • @Sajeetharan。我不想输出所有HTML。我想收到短信my second note。您所做的是显示所有 HTML 文本
  • 另外,如果您注意到我的观点,如果签入控制器发现note2,我想显示my second note。有什么想法得到这个my second note 文本?
  • 那些投票支持这个答案的人不理解我的问题
【解决方案3】:

改成

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");
}

【讨论】:

  • 向 OP 询问,为什么 OP 使用循环?
  • 因为我的真实数据是循环的。这就是为什么我在循环中给出示例JASON
猜你喜欢
  • 2013-06-19
  • 1970-01-01
  • 1970-01-01
  • 2011-09-04
  • 2014-07-07
  • 1970-01-01
  • 2016-02-13
  • 2017-07-30
  • 2018-10-16
相关资源
最近更新 更多