2014-10-01 24 views
0

<ul class="alert alert-link alert-info" 
    data-ng-show="messages"> 
    <li data-ng-repeat="msg in messages" data-ng-bind-html="msg | unsafe"></li> 
</ul> 

控制器代码看起来如下:

... 
var msg = 'You\'re logged in. The registration must ' + 
      + 'be <a href="#" ' + 
      + 'data-ng-click="logout(\'/ajax/logout/\', $event)">logout</a> of your profile.'; 
$scope.messages = [msg]; 
... 

如何$compile一个msg,如果msg来自服务器?

回答

0

一般来说,直接从后端编译角度是非常可怕的想法。你通常可以告诉你,如果你在你的代码中写下了unsafe这个词,那么你就处于危险之中。更不用说如果代码位于某个其他服务器/模型/数据库中,将很难找到代码问题。您应该返回可以从服务器使用的模型。

如果你必须这样做,你可能会想要在你的链接函数中创建一个类似element.html($compile(msg)(scope))的指令。

+0

这是i18n消息,它存储在后端的静态文件中。 \t 您的解决方案会抛出一个错误:'错误:[$ sce:itype] ...' [** plunk **](http://plnkr.co/edit/sZARES?p=preview) (),'append()',... – romeo 2014-10-01 19:22:14

+0

通常,停止使用指令而不是'ng-repeat': 'element.html($ compile('

  • '+ messages.join('
  • ')+'
  • ')($ scope));'' – romeo 2014-10-01 21:51:58