3

I'm trying to pass an object from jade to ng-init in angular

This: doesn't work:

ng-init='tables=!{JSON.stringify(tables)}'

This: expands but,

ng-init='tables=#{JSON.stringify(tables)}'

the output is unescaped and filled with "s

ng-init="tables={"12":{"id":....

and the view isn't updated in either of the cases. This article implies that first one should work, but like I said, it doesn't even expand,

ng-init='tables=!{JSON.stringify(tables)}'

in source code shows up exactly the same in the HTML source

ng-init='tables=!{JSON.stringify(tables)}'
laggingreflex
  • 32,948
  • 35
  • 141
  • 196

2 Answers2

5

Actually, the #{...} approach seems to work fine.
It is probably the way console.log prints attributes' values that confused you.

ng-init="tables=#{JSON.stringify(tables)}"

Take a look at this short demo.

gkalpak
  • 47,844
  • 8
  • 105
  • 118
  • In a custom directive, `ng-init` is not executed in the `controller` but in the `link` function. I ran into this again today and I was trying to access ng-init-ed variables from the controller instead of the link. – laggingreflex Dec 20 '14 at 16:13
-1

In what use-case you want to pass data directly from Jade to angular? I think you could to this job in controller like this :

$scope.init = function () { // init stuff }

...and in your view :

ng-init = init()

cvng
  • 1,822
  • 17
  • 23