yotle
yotle

Reputation: 71

Passing a string from flask to angularjs

I pass a variable from flask to a template like this

    @app.route('/')
    def respond():
        return render_template('index.html', bar='foo')

In angularjs controller attached to the body of that page I have a function like this:

$scope.init = function(bar){
        $scope.content = bar;
    }

I am trying to pass this variable from flask to angular in the template like this:

<body ng-init="init({{ bar|tojson|safe }})" ng-controller="AppCtrl">

But in the end the tag in DOM is messed up

<body ng-init="init("foo")" ng-controller="AppCtrl">

The problem with this are quotation marks. Browser cannot interpret them properly and the variable isn't passed.

So my question is how can I solve this? I don't want to make another http request from angular controller just to get that data.

edit.

<body ng-init="init('{{ bar }}')" ng-controller="AppCtrl">

seems to have fixed it

Upvotes: 1

Views: 1853

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136154

Simply use ' while DOM attribute already has started with "

Double inside double qoute will break the DOM.

<body ng-init="init('{{ bar|tojson|safe }}')" ng-controller="AppCtrl">

Will render on DOM as

<body ng-init="init('foo')" ng-controller="AppCtrl">

Hope this could help you. Thanks.

Upvotes: 1

Related Questions