How to render html with AngularJS templates

后端 未结 5 713
遥遥无期
遥遥无期 2020-11-27 15:31

This is my template:

a

5条回答
  •  清酒与你
    2020-11-27 16:01

    You shoud follow the Angular docs and use $sce - $sce is a service that provides Strict Contextual Escaping services to AngularJS. Here is a docs: http://docs-angularjs-org-dev.appspot.com/api/ng.directive:ngBindHtmlUnsafe

    Let's take an example with asynchroniously loading Eventbrite login button

    In your controller:

    someAppControllers.controller('SomeCtrl', ['$scope', '$sce', 'eventbriteLogin', 
      function($scope, $sce, eventbriteLogin) {
    
        eventbriteLogin.fetchButton(function(data){
          $scope.buttonLogin = $sce.trustAsHtml(data);
        });
      }]);
    

    In your view just add:

    
    

    In your services:

    someAppServices.factory('eventbriteLogin', function($resource){
       return {
            fetchButton: function(callback){
                Eventbrite.prototype.widget.login({'app_key': 'YOUR_API_KEY'}, function(widget_html){
                    callback(widget_html);
                })
          }
        }
    });
    

提交回复
热议问题