Unable to use global variable inside foreach

73 views
Skip to first unread message

Krupakar Reddy

unread,
Mar 10, 2015, 8:13:10 AM3/10/15
to knock...@googlegroups.com
Hi,

i am trying to access a global variable inside a  <!-- ko foreach: entities -->

    <td data-bind="text:$root.isGroupValid"></td>

isGroupValid is global variable but while rendering to the table it is not showing any value any any clues on why its not showing values for the variable


Gunnar Liljas

unread,
Mar 10, 2015, 8:17:12 AM3/10/15
to knock...@googlegroups.com
What do you mean by "global variable"? $root refers to the viewmodel sent into ko.applyBindings.

--
You received this message because you are subscribed to the Google Groups "KnockoutJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knockoutjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Krupakar Reddy

unread,
Mar 10, 2015, 8:29:06 AM3/10/15
to knock...@googlegroups.com
 isGroupValid is a variable which is not a part of entities object .i am able to perfectly use the isGroupValid variable ooutside of the  <!-- ko foreach: entities --> tag

Gunnar Liljas

unread,
Mar 10, 2015, 8:31:52 AM3/10/15
to knock...@googlegroups.com
Remove $root then, but it's really not a good practice.

/G

--

Krupakar Reddy

unread,
Mar 10, 2015, 8:34:48 AM3/10/15
to knock...@googlegroups.com
if remove the $root table is not at all generating the content with $root i am able to build the table but value is not populated i am totally confused

Noirabys

unread,
Mar 19, 2015, 8:53:01 AM3/19/15
to knock...@googlegroups.com
please supply more code ...

in foreach you can access the parent context with $parent 

$root is the modelx object you supply by ko.applyBindings(modelx,nodey) when you are in the dom-tree supplied by nodey, default for nodey is the document body

if you don't supply a context i think knockout tries to find it in $data , if not found there knockout tries the global (window) object 
Reply all
Reply to author
Forward
0 new messages