简体   繁体   English

动态ng-init-AngularJS

[英]Dynamic ng-init - Angularjs

I got this: 我懂了:

<ul id="filterList">
      <div ng-repeat="data in dataForTheTree">
         <li> {{data.topic}} </li>
          <ul id={{data.topic}}Son>
              <li ng-repeat="d in data.children>  
                 <input type="checkbox" ng-init='{{d.subtopic}}Checkbox = true' ng-checked = "{{d.subtopic}}Checkbox"/>{{d.subtopic}}
              </li>
          </ul>
      </div>
</ul>

I need to create a "model" to know if the checkbox is checked or not (i can not use ng-model because is a bad practice). 我需要创建一个“模型”来知道是否选中了该复选框(我不能使用ng-model,因为这是一种不好的做法)。 So, I am trying with ng-checked. 因此,我正在尝试ng-checked。 ng-checked will not work if i can not initialise it. 如果我无法初始化,ng-checked将无法正常工作。 I want to try with ng-init because is the only thing that i have read about it. 我想尝试ng-init,因为这是我唯一读过的内容。

I have read something about ng-init in a similar post, but i can not understand because I am so new in this world. 我已经在类似的文章中读到了有关ng-init的内容,但我无法理解,因为我在这个世界上很新。

"You need to access dynamic properties using [] notation instead on . notation" “您需要使用[]表示法而不是。表示法来访问动态属性”

How can i do that in this example? 在这个例子中我该怎么做?

This is my .Json: 这是我的.Json:

$scope.dataForTheTree =[
  { "topic" : "Legislation", "children" : [
      { "subtopic" : "Education", "texto" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae tincidunt mi. Donec purus ligula, tincidunt ut semper eu, laoreet.", "children" : [] },
      { "subtopic" : "HealthService", "texto" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae tincidunt mi. Donec purus ligula, tincidunt ut semper eu, laoreet.", "children" : [] },
      { "subtopic" : "Assets", "texto" : " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae tincidunt mi. Donec purus ligula, tincidunt ut semper eu, laoreet.", "children" : [] },
      { "subtopic" : "LiquidAssets", "texto" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae tincidunt mi. Donec purus ligula, tincidunt ut semper eu, laoreet.", "children" : [] },
      { "subtopic" : "Education", "texto" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae tincidunt mi. Donec purus ligula, tincidunt ut semper eu, laoreet.", "children" : [] },
      { "subtopic" : "HealthService", "texto" : " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae tincidunt mi. Donec purus ligula, tincidunt ut semper eu, laoreet.", "children" : [] },
      { "subtopic" : "HealthService", "texto" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae tincidunt mi. Donec purus ligula, tincidunt ut semper eu, laoreet.", "children" : [] }
]}];

I want to link each checkbox with his subtopic, then I want to watch if the checkbox is checked through ng-checked = "{{d.subtopic}}Checkbox" . 我想将每个复选框与其子主题链接起来,然后我想看看是否通过ng-checked = "{{d.subtopic}}Checkbox"了该复选框。

Now my problem is that "{{d.subtopic}}Checkbox" is not initialised, and that is why I need to use ng-init. 现在我的问题是"{{d.subtopic}}Checkbox"没有被初始化,这就是为什么我需要使用ng-init的原因。

The problem here is that ng-init does not understand {{}}, so I do not know how to solve this: '{{d.subtopic}}Checkbox = true' 这里的问题是ng-init无法理解{{}},因此我不知道该如何解决: '{{d.subtopic}}Checkbox = true'

Finally I just want to get a dynamic $scope, like for example: 最后,我只想获得一个动态的$ scope,例如:

var cad = subtopic+"Checkbox";

  if($scope[cad] === true){ [...]

Is this what you are trying to achive? 这是您要达到的目标吗?

<ul id="filterList">
  <div ng-repeat="data in dataForTheTree">
    <li> {{data.topic}} </li>
    <ul id="{{data.topic}}Son">
      <li ng-repeat="d in data.children">
        <input type="checkbox" ng-init='d.Checked = true' ng-model="d.Checked" />
        {{d.subtopic}}
        <span ng-if="d.Checked">{{d.texto}}</span>
      </li>
    </ul>
  </div>
</ul>

or this? 或这个?

    <li ng-repeat="d in data.children">
      <input type="checkbox" ng-init="$scope[d.subtopic+'Checked'] = true" 
          ng-model="$scope[d.subtopic+'Checked']" />
      {{d.subtopic}}
      <span ng-if="$scope[d.subtopic+'Checked']">{{d.texto}}</span>
    </li>

plunker example 矮人的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM