The angular made scope is available in Django TemplateResponse ()

I use it Python/Django

as a back-end for the framework and angular

as a front-end. At some point I use TemplateResponse()

in a view function to get a template response, this template includes the code HTML/CSS/JS/Angular

. TemplateResponse()

can execute any other code but not angular bec angular scope is not available.

Here's some sample code:

Django code

 t = TemplateResponse(request,
                             'dynamic-content.html',
                            {'raw_html':raw_html,
                            'raw_css':raw_css,
                            'raw_js':raw_js,
                            'js_script':js_script,
                            'css_script':css_script,})
t.render()

      

Now I will post t.content

to angular that will include the template response. In angular, I will be using the directive to create angular scope in the above template answer.

<div dynamic="t.content"></div>

      

Directive:

app.directive('dynamic', function ($compile) {
  return {
    restrict: 'A',
    replace: true,
    link: function (scope, ele, attrs) {
      scope.$watch(attrs.dynamic, function(html) {
        ele.html(html);
        $compile(ele.contents())(scope);
      });
    }
  };
});

      

But b / w time providing angular scope for template response takes time. Another problem: will js

compile to angular scope.

So either I have to make the scope angular

available in the Template or stop js

to compile before angular. What approach should I take in this regard. Please, help.

0


source to share





All Articles