简体   繁体   English

Angular.js在ng-bind-html中嵌套花括号

[英]Angular.js nesting curly braces in ng-bind-html

I am making a spelling app using JavaScript and Angular.js. 我正在使用JavaScript和Angular.js制作拼写应用。 When the user has spelled their word correctly, the view should say "Word already completed!", and when the user has not yet completed their word, the view should display a list of friends. 当用户正确拼写了他们的单词时,视图应显示“单词已经完成!”,而当用户尚未完成其单词时,视图应显示朋友列表。

Therefore, I need to change the innerHTML of the object from within my JS script (so that it updates when the user spells their word correctly). 因此,我需要在我的JS脚本中更改对象的innerHTML(以便在用户正确拼写单词时更新该对象)。

This is the if statement that I use to decide between the two innerHTMLs: 这是我用来确定两个innerHTML之间的if语句:

if(wordCompletedVar) {
  $scope.friendsHTML = "<li><a> Word already completed! </a></li>";
} else {
  $scope.friendsHTML = "<li class='user-display' style='padding: 15px' ng-repeat='friend in friendNames track by $index'><img  src='{{profilePicture($index)}}' class='img-circle user-display-img'><div class='user-display-name'><a href = '#/trade/{{getFriendUsername($index)}}' ng-click='playSound('whoosh')' style = 'color: white'>{{friend}}</a></div></li>";
}

In my HTML doc, I am trying to inject friendsHTML using ng-bind-html: 在我的HTML文档中,我尝试使用ng-bind-html注入friendsHTML:

<ul class="dropdown-menu" id="friendsDropdown" ng-bind-html = "to_trusted(friendsHTML)"></ul> 

It works with the simple sentence, but with the more complicated HTML (with nested Angular.js curly brackets {{}}), the brackets display instead of the angular object (ie {{friends}}). 它适用于简单的句子,但适用于更复杂的HTML(带有嵌套的Angular.js大括号{{}}),而不是尖锐的对象(即{{friends}})显示括号。 The same happened when I used ng-bind-html without the to_trusted function. 当我使用不带to_trusted函数的ng-bind-html时,也会发生同样的情况。

This is a clearer view of the HTML I am trying (and failing) to inject: 这是我尝试(但失败)注入的HTML的更清晰视图:

<li class='user-display' style='padding: 15px' ng-repeat='friend in friendNames track by $index'>
  <img  src='{{profilePicture($index)}}' class='img-circle user-display-img'>
  <div class='user-display-name'>
    <a href = '#/trade/{{getFriendUsername($index)}}' ng-click='playSound('whoosh')' style = 'color: white'>{{friend}}</a>
  </div>
</li>

The to_trusted function that I am using is: 我正在使用的to_trusted函数是:

$scope.to_trusted = function(html_code) {
  return $sce.trustAsHtml(html_code);
};

(and I do include $sce in my controller). (并且我的控制器中确实包含$ sce)。

How can I nest the curly-bracket angular notation within the ng-bind-html injection? 如何在ng-bind-html注入中嵌套花括号角度符号? I have found a lot of documentation on either the curly brackets or ng-bind-html, but I haven't seen anything on how to use them together (or how to accomplish what I am trying to accomplish in a different way-I am open to suggestions!) 我已经在大括号或ng-bind-html上找到了很多文档,但是关于如何一起使用它们(或者如何以其他方式完成我要完成的工作),我还没有看到任何东西。欢迎提出建议!)

The best way to handle this would be to avoid injecting HTML at all, and instead use a conditional statement like ng-if or ng-show . 处理此问题的最佳方法是完全避免注入HTML,而应使用ng-ifng-show类的条件语句。

something like the following: 类似于以下内容:

<li ng-if="wordCompletedVar"><a> Word already completed! </a></li>

<li ng-if="!wordCompletedVar" class='user-display' style='padding: 15px' ng-repeat='friend in friendNames track by $index'>
  <img  src='{{profilePicture($index)}}' class='img-circle user-display-img'>
  <div class='user-display-name'>
    <a href = '#/trade/{{getFriendUsername($index)}}' ng-click='playSound('whoosh')' style = 'color: white'>{{friend}}</a>
  </div>
</li>

An alternative would be along the lines of making a directive as below. 另一种选择是遵循以下指令。 You can modify parameters if you want reuse etc - 如果您想重复使用,可以修改参数-

app.directive('userDisplay', ['$compile',function ($compile) {
    return {
        template : '<input/>',
        link: function(scope, element, attrs) {

            scope.$watch('wordCompletedVar',function(){
                var html;
                if(scope.wordCompletedVar) {
                    html = "<li><a> Word already completed! </a></li>";
                  } else {

                    html= "<li class='user-display' style='padding: 15px' ng-repeat='friend in friendNames track by $index'><img  class='img-circle user-display-img'><div class='user-display-name'><a href = '#/trade/{{getFriendUsername($index)}}' ng-click='playSound(\"whoosh\")' style = 'color: white'>{{friend}}</a></div></li>"

                  }
                  element.html(html);
                  $compile(element.contents())(scope);

        });
    }
    };
}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM