skip to Main Content

i’m using Angularjs and I have a ng-view with some parts. The partial views have some scripts in the head.

<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
...
</head>
<body>

	<div class="section">
      ... some content
	</div>
</body>
</html>

If i load a view those scripts are loaded too.

<div style="" class="ng-scope" ng-view="">
  <meta class="ng-scope" charset="utf-8">
  <script class="ng-scope" type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
  <div class="section ng-scope">
    .. some view content.
  </div>
</div>
  

I’m using an HTML Editor for Twitter Bootstrap to edit views quickly and this tool needs the html, head and body tags. And I don’t want to remove them from templates. Is it posible to ignore the head tag of partial view when it’s loaded?

2

Answers


  1. Chosen as BEST ANSWER

    I got it. I took a look into templateRequestProvider and httpProvider. And Here is my solution.

    app.config(function($routeProvider, $httpProvider) {
    
      ....		
    
      function appendTransform(defaults, transform) {
    
        // We can't guarantee that the default transformation is an array
        defaults = angular.isArray(defaults) ? defaults : [defaults];
    
        // Append the new transformation to the defaults
        return defaults.concat(transform);
      }
      /**
      * My HTML Template transformation
      */
      function doTransform(value){
        if(angular.isString(value)){
          return value.replace(/<head>(.|n)*</head>/mg,"");
        }
        return value;
      }
    
      $httpProvider.defaults.transformResponse = appendTransform($httpProvider.defaults.transformResponse,
        function(value) {
          return doTransform(value);
      });
    
    })

    It's not a best solution because it would filter each http request. Better to apply it only for ng-view. It should work with templateRequestProvider.httpOptions but only with angular 1.5+.


  2. Instead of using a ng-view, isn’t better to create a directive with your template in, and use the compile() or in the link() phase to remove the bad tags ?

    Afaik directives are the good way to alter the DOM.
    I’m not really familiar with the compile() but I let you read this post if you are interested in : http://www.bennadel.com/blog/2794-when-do-you-need-to-compile-a-directive-in-angularjs.htm

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search