Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS:我显然不理解ng init的用法_Javascript_Angularjs - Fatal编程技术网

Javascript AngularJS:我显然不理解ng init的用法

Javascript AngularJS:我显然不理解ng init的用法,javascript,angularjs,Javascript,Angularjs,在AngularJS控制器中,我有一个非常分层的JSON结构作为范围变量。我想循环该变量的不同部分。我考虑使用nginit来指定我在结构中的位置。下面是一些代码: my_app.js: my_template.html: 我希望这会显示一个列表: a、 a.a a、 a.b 但它什么也没显示 当然,如果我在things.children[0].children中显式指定循环ng repeat=thing,它就可以正常工作。但是,这段小小的模板代码必须在我的应用程序的不同层次上的不同点上运行 只是

在AngularJS控制器中,我有一个非常分层的JSON结构作为范围变量。我想循环该变量的不同部分。我考虑使用nginit来指定我在结构中的位置。下面是一些代码:

my_app.js:

my_template.html:

我希望这会显示一个列表:

a、 a.a a、 a.b 但它什么也没显示

当然,如果我在things.children[0].children中显式指定循环ng repeat=thing,它就可以正常工作。但是,这段小小的模板代码必须在我的应用程序的不同层次上的不同点上运行

只是为了让生活变得复杂,我可以使用标准JavaScript或Django的聪明来获得当前的东西级别

有什么想法吗?

ng init的运行优先级低于ng repeat 1000。因此,当放在同一个元素上时,首先编译ng repeat,这意味着在执行ng repeat之前,不会定义由ng init创建的scope属性

因此,如果希望以这种方式使用它,则需要将其放置在父元素上

<div ng-app="my_app" ng-controller="MyController">
  <ul ng-init="current_thing=things.children[0]">
    <li ng-repeat="thing in current_thing.children>
      {{ thing.name }}
    </li>
  </ul>
</div>

非常感谢。修正了这个玩具的例子。不过,它在真正的代码中仍然不起作用。我需要再考虑一下,也许会给你回电话。我的荣幸。原则应该是一样的。但是请记住,nginit是有代价的,这主要是调试。在备份控制器中通过适当的数据结构调试ng init要困难得多。
<div ng-app="my_app" ng-controller="MyController">
  <ul>
    <li ng-init="current_thing=things.children[0]" ng-repeat="thing in current_thing.children>
      {{ thing.name }}
    </li>
  </ul>
</div>
<div ng-app="my_app" ng-controller="MyController">
  <ul ng-init="current_thing=things.children[0]">
    <li ng-repeat="thing in current_thing.children>
      {{ thing.name }}
    </li>
  </ul>
</div>