【问题标题】:angularjs:-Append data into HTML the angular wayangularjs:-以角度方式将数据附加到 HTML
【发布时间】:2014-10-27 22:22:53
【问题描述】:

我是 Angular 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 没有正确显示而出现问题。另外我不知道我哪里出错了。

我只是想知道“角度”的做法。

谢谢。

【问题讨论】:

    标签: jquery html angularjs angularjs-directive append


    【解决方案1】:

    请阅读这个http://lostechies.com/gabrielschenker/2013/12/28/angularjspart-6-templates/ :)) 有解释如何使用模板的好工作:) 由 ng-template

    【讨论】:

    • 如何使用 ng-include 以及如何追加到下一节。
    猜你喜欢
    • 2019-05-13
    • 1970-01-01
    • 2018-03-27
    • 2017-12-15
    • 1970-01-01
    • 2015-04-13
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多