簡體   English   中英

AngularJS將文本轉換為HTML標簽

[英]AngularJS Convert Text to HTML Tags

好的,我正在學習AngularJS,其中一項功能是允許用戶在文本區域中鍵入HTML代碼,然后將其發送到Controller並存儲在代碼變量中。 當用戶按下按鈕來測試代碼時,我希望將文本插入HTML文檔中。 當前,它所做的只是將其添加為純文本,而不是呈現它。

test-code.template.js:

<button type="button" class="btn btn-primary" ng-click="$ctrl.showCode()">Test Code</button>
<button type="button" class="btn btn-primary" ng-click="$ctrl.clearCode()">Reset All</button>

{{$ctrl.codeShow}}

test-code.component.js:

angular.
  module('phonecatApp').
  component('testCode', {
    templateUrl: 'Components/test-code/test-code.template.html',
    controller: [function TestCodeController() {
      var self = this;

  self.code = '';
  self.codeShow = '';

  self.showCode = function showCode()
  {
    self.codeShow = self.code;
  }

  self.clearCode = function clearCode()
  {
    self.codeShow = '';
    self.code = '';
  }

  }]
  });

只是澄清一下,按一下按鈕就可以了,按一下按鈕,數據就成功地從代碼添加到codeShow了,它可以顯示它,但是它以明文形式顯示而不是呈現。 謝謝

嘗試

 <div ng-bind-html="$ctrl.codeShow"></div>

另外,請參閱https://docs.angularjs.org/api/ngSanitize/service/ $ sanitize以獲得更好的實現

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM