【问题标题】:AngularJS arhitectureAngularJS 架构
【发布时间】:2015-11-04 03:59:06
【问题描述】:

我有以下应用程序,我在其中循环并显示一些数据。

<div class="thing" ng-repeat="thing in things">
          <h1>{{thing.title}}</h1>
          <p>{{thing.description}}</p>
          <br/>
          <div class="child">
              <!-- ng-repeat="child in ??? -->
              <p>Display child here</p>
          </div>

      </div>

我的数据:

$scope.things = [{
    id: 1,
    title: 'First thing',
    description: 'This is just a thing',
    extra: {
        childs: 3 /* the id of the children */
    }
}, {
    id: 2,
    title: 'Second thing',
    description: 'This is just a thing',
    extra: {}
}, { /* dont displat this object in the ng-repeat="thing in things" */
    id: 3,
    title: 'Child thing',
    description: 'This is a child thing',
    extra: {}
}]

我不明白它如何 ng-repeat 只是 THINGS that are NOT CHILDREN 并在 .child div 中显示 ID 为 thing.extra.childs 的元素。

谁能解释我如何做到这一点?

http://jsfiddle.net/U3pVM/18350/

【问题讨论】:

  • 好的,我得到了它正在寻找第一个元素 child = 3 的问题是指的是 id=3 的 JSON 对象,您不想重复这一点。你能改进你的 JSON 吗?
  • 我同意@joyBlanks,你应该尝试改进你的 JSON 数据结构,这会让事情变得更容易。
  • 有什么建议吗?数据来自其他 API,我可以添加 child: { all the data from id:3 } 但我使用的是 API
  • 我猜当你收到数据迭代并将子对象移动到额外的键中时。即解决参考。无论如何,角度会迭代,但它会顺利迭代

标签: javascript angularjs architecture


【解决方案1】:

您肯定需要变形数据对象。您也不希望孩子显示在列表中。基本上迭代并让子对象移动到额外的键中。即解析引用

** 你不想在显示中处理复杂的逻辑,所以在你的模型中你应该做所有脏活。由于您无法更改 API,我们将更改 API 参考

在您的控制器中,我猜这将来自 API,假设您在变量 data 中有 JSON。

//data will be coming from API
/*data = [{
    id: 1,
    title: 'First thing',
    description: 'This is just a thing',
    extra: {
        childs: 3 
    }
}, {
    id: 2,
    title: 'Second thing',
    description: 'This is just a thing',
    extra: {}
}, { 
    id: 3,
    title: 'Child thing',
    description: 'This is a child thing',
    extra: {}
}]; */

然后您可以在收到数据的下方修改此 JSON,并将此代码放入您的控制器中。

for(var i in data){
  if(data[i].extra && data[i].extra.childs){
    for(var j in data){
      if(data[j].id == data[i].extra.childs){
        data[i].child = data[j];
        delete(data[j]);
        break;
      }
    }
  }
}

$scope.things = data;//then put the morphed JSON in the $scope.things

HTML 将是这样的。请注意,我在您的 JSON 中添加了一个密钥 child,他有孩子

<div class="thing" ng-repeat="thing in things">
    <h1>{{thing.title}}</h1>
    <p>{{thing.description}}</p>
    <br/>
    <div class="child" ng-if="things.child">
        <p>{{thing.child.title}}</p>
        <p>{{thing.child.description}}</p>
    </div>
</div>

【讨论】:

    【解决方案2】:

    您应该为此目的使用 ng-if 指令:

    <div ng-app>
      <div ng-controller="MainCtrl">
    
          <div ng-repeat="thing in things">
              <h1>{{thing.title}}</h1>
              <p>{{thing.description}}</p>
              <p ng-if="thing.extra.childs">
                  {{thing.extra.childs}}
              </p>
          </div>
    
        </div>
    </div>
    

    JsFiddle : http://jsfiddle.net/nikdtu/op4j7jhj/

    【讨论】:

    • 我认为这不是 OP 想要的,请检查问题下的 cmets。
    • 没错,我想要对象,我知道要显示 ID
    【解决方案3】:
        <div class="thing" ng-repeat="thing in things">
            <h1>{{thing.title}}</h1>
            <p>{{thing.description}}</p>
            <br/>
            <div ng-repeat="(key, value) in thing.extra" class="child">
                <p>{{ key }} : {{ value }}</p>
            </div>
        </div>
    

    【讨论】:

    • 我想要对象,我知道要显示 ID
    • 哪个对象?没看懂
    • 您想在 html 中显示键值对吗?
    • 是的,在事物中不要取代孩子的事物
    • 我刚刚用带有键值对的 ng-repeat 更新了 sn-p。这就是你要找的吗?
    猜你喜欢
    • 2015-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多