angularjs: - 将数据以角度方式转换为HTML

时间:2014-09-03 12:10:21

标签: jquery html angularjs angularjs-directive append

我是角度js的新手,我有一个js背景并陷入困境: -

我希望在指令的帮助下将数据附加到HTML中。

我将值(即HTML)存储在一个字符串中。但是它给出了值。其中一个是转义字符串。 不知怎的,我无法实现它。

以下是我的代码。

woi.directive("addbuttons",['$rootScope','userAPI', function($rootScope,userAPI,$compile){
    return function(scope, element, attrs){
        element.bind("click", function(){
            scope.count++;
            //var abc= scope.loadRated(pageno);
            userAPI.topRated({userid: $rootScope.getUser().userid}, function (r, $scope, div){
                if(!$rootScope.device.isMobile && !$rootScope.device.isTablet && !$rootScope.device.isTouch ) {
                    var topRatedList = r.gettopratedhomepage.topratedprogrammelist;
                    var str='';
                    for(var i=0;i<topRatedList.length;i++)
                    {
                        str+=topRatedList[i].actualname;

                      var str="
                       <div class='thumb'>
                        <div ng-show='topRatedList[i].isrecommended==1' class='favorite-ribbon'></div>
                        <div class='player'></div>
                        <div class='image' style='background-image:url({{topRatedList[i].imagefilepath}});'>
                            <a ng-click='playVideo(topRatedList[i].programmename,$event)'  ng-show='hasVideo()' class='play' style='cursor:pointer'></a>

                            <a ng-href='#!/program/{{topRatedList[i].programmename | encodeUrl}}'  ng-show='!hasVideo()' class='noPlay' ng-click='EncodeUrlWithDash(topRatedList[i].programmename,$event,'programme',topRatedList[i].channelid,topRatedList[i].programmeid,topRatedList[i].starttime)'></a>
                        </div>

                        <span class='time' ng-show='topRatedList[i].duration'>{{topRatedList[i].duration | videoLength}}</span>

                        <div class='user-actions' ng-controller='UserController'>

                            <a live-tooltip='Add to Favorite'  ng-click='toggleFavorite(p, $event)'  class='btn btn-small btn-purple-blue' ng-class='{active:topRatedList[i].isfavorite == \'1'}">
                                <i class="icon-favorite"></i>
                            </a>

                            <a live-tooltip="Reminder Alerts"  ng-click='toggleReminder(p, $event)'  class='btn btn-small btn-purple-blue' ng-class="{active:topRatedList[i].isreminder == '1'}"  >
                                <i class="icon-reminder"></i>
                            </a>

                            <a live-tooltip='Add to Watchlist' ng-click='addToWatchlist(p, $event)'  class='btn btn-small btn-purple-blue' ng-class="{active:topRatedList[i].iswatchlist == '1'}">
                                <i class="icon-watchlist"></i>
                            </a>
                        </div>

                    </div>
                        <div class="text-wrapper">
                        <h2 multiline-overflow><a title="{{topRatedList[i].programmename}}" href="#!/program/{{topRatedList[i].programmename | encodeUrl}}" ng-click="EncodeUrlWithDash(topRatedList[i].programmename,$event,'programme',topRatedList[i].channelid,topRatedList[i].programmeid,topRatedList[i].starttime)" >{{topRatedList[i].programmename}}
                        </a></h2>

                        <p class='infoChannel' live-tooltip-single-line= "{{topRatedList[i].channeldisplayname}}"><a href="#!/channel/{{topRatedList[i].channeldisplayname | encodeUrl}}" ng-click="EncodeUrlWithDash(topRatedList[i].channeldisplayname,$event,'channel',topRatedList[i].channelid,topRatedList[i].programmeid)">{{topRatedList[i].channeldisplayname}}
                    </a></p>
                        <p class='info'>{{topRatedList[i].starttime|featuredVideoTime}}</p>

                        </div>";
                    }

                    angular.element(document.getElementById('space-for-buttons')).append($compile("<div><button class='btn btn-default' data-alert="+scope.count+">Show alert #"+scope.count+"</button></div>")(scope));
                }
            });
        });
    };
}]);

我从userApi获取数据,并希望将其放入Html。

以下是我试图实现点击事件的html代码: -

 <div class='tab clearfix' ng-show="userLogged=='off' && currentActiveTab == 'rated'" >

          <div >
            <input type="hidden" id="hdnTopRatedPage" value="0" ng-update-hidden>
          <div class='item' ng-repeat='p in programs.rated' home-tab-item watchable="p" ></div>
          </div>
          <div id="space-for-buttons">


          <div class = 'rated-spin' ng-show = 'programs.rated == 0 && ratedLoaded == false'> </div>
          <div ng-show="programs.rated == 0  && ratedLoaded == true" class="noResultsBlock">No Results for Rated.</div>
          <div class='clearfix'></div>
          </div>

          <div ng-controller="MainCtrl" ng-app="woi">
            <yes style='background-color: brown;padding-right: 61px;'></yes>

           </div>

        </div>

上面的代码也没有用,因为我得到的div没有正确显示问题。我也不知道我哪里出错了。

我只想知道&#34; angular&#34;这样做的方法。

感谢。

1 个答案:

答案 0 :(得分:0)

请阅读此http://lostechies.com/gabrielschenker/2013/12/28/angularjspart-6-templates/ :))通过ng-template解释如何使用模板的好工作:) enter image description here