abhivij
abhivij

Reputation: 107

Assign jade variable to Angular

In my application, I'm using NodeJS, Express in the backend and Angular in the frontend. I'm also using Jade template engine.

jade obtains a variable called "adv" by this code.

      res.render('page',{adv:result[0]})

In controller.js (for angular)

        $scope.content = [];

I would like to do something like

        form(ng-init="content=#{adv}") 

            h5 {{"content" + content}}

i.e. assign that jade variable to the scope. It is not working. I could use http.get and get the content directly to angular scope, but just wondering if it is possible to do this

Thanks

Upvotes: 2

Views: 2225

Answers (1)

abhivij
abhivij

Reputation: 107

This worked.

    - var str_adv = JSON.stringify(adv)

    form(ng-init="content = #{str_adv}") 

or even (where getContent is a function defined in controller doing the same thing)

    form(ng-init="getContent(#{str_adv})") 

Both these also parsed the string and stored the object in 'content'

But directly using

    form(ng-init="content = JSON.stringify(#{adv})")

gives the same error

It seems assigning objects in ng-init is not possible.

Thanks @ivarni for the hint about stringify

Upvotes: 3

Related Questions