chairuosen
V2EX  ›  问与答

Angular 中手工动态插入的 html,如何能让 angular 解析 ng 标签以及$scope 变量等动态内容

  •  
  •   chairuosen · Dec 3, 2014 · 13702 views
    This topic created in 4357 days ago, the information mentioned may be changed or developed.
    我知道手动插入html违反angular模式,被迫如此是因为Html模板中模块的顺序是由数据顺序决定(附1),非简单的ng-repeat可以解决(问题1),所以决定把他放在服务端来拼。
    但是拼起的html用ng-bind-html插入页面后,html中的angular标签或者{{content}}都不起作用了。如何能够让其被重新解析一遍(问题2)?
    ---
    求解 问题1 || 问题2

    附1:数据是类似`[{type:3},{type:1},{type:3}]`要求输出`<div type=3></div><div type=1></div><div type=2></div>`
    8 replies  •  2014-12-04 00:31:24 +08:00
    ZackYang
    ZackYang
    Dec 3, 2014
    1
    $compile
    sd4399340
    sd4399340
    Dec 3, 2014 via iPhone
    2
    确实不符合angular的实践,建议用directive 封起来
    EPr2hh6LADQWqRVH
    EPr2hh6LADQWqRVH
    Dec 3, 2014
    3
    我记得跟$sce有关
    phyng
    phyng
    Dec 3, 2014 via Android
    4
    $compile($(#id))($scope)
    chairuosen
    chairuosen
    OP
    Dec 3, 2014
    5
    @avastms 跟sce无关,sce只是过滤掉不安全的标签。

    @phyng @ZackYang 感谢,明天试试
    phyng
    phyng
    Dec 3, 2014
    6
    @chairuosen 最近经常用,有个问题我一直没解决,就是填充ng-bind-html之后立刻执行$compile是无效的,我是用$timeout延时100ms再$compile实现= =
    p2p
    p2p
    Dec 3, 2014 via iPhone
    7
    用 directive
    chairuosen
    chairuosen
    OP
    Dec 4, 2014
    8
    @phyng 因为ng-bind是类似setTimeout(checkAllBindingItem,1);这样一种实现
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1695 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 10:29 · PVG 18:29 · LAX 02:29 · JFK 05:29
    ♥ Do have faith in what you're doing.