使用AngularJS附加HTML [英] Append HTML with AngularJS

查看:68
本文介绍了使用AngularJS附加HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTML

<section>
  <div>
     <p>{{item}}</p>
  </div>
</section>

controller.js

controller.js

(function () {
   var ctrl = function ($scope){
        $scope.item = "test";
   }
}());

这是我团队的项目。我无法改变它的设置方式。我试图解决的问题是将$ scope.item插入到html中。我怎么能这样做?

This is my team's project. I cannot change how it is set up. The problem I am trying to solve is inserting the $scope.item into the html. How could I do this?

推荐答案

我在章节中添加了一个id _item。

I added an id _item in section.

<section id="_item">
  <div>
     <p>{{item}}</p>
  </div>
</section>

将以下代码写在controller.js文件中。

Write this below code inside the controller.js file.

angular.element(document.getElementById('_item')).append($compile("<div>
     <p>{{item}}</p>
  </div>")($scope));

注意:添加依赖$ compile。

Note: add dependency $compile.

我认为这对你有帮助。

这篇关于使用AngularJS附加HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆