【问题标题】:How to pass mutliple $scope values in ng-repeat angularjs?如何在 ng-repeat angularjs 中传递多个 $scope 值?
【发布时间】:2018-04-30 19:44:27
【问题描述】:

这是我的 json 数据:

{
    "seendata": {
        "count": 2,
        "rows": [
            {
                "id": 8,
                "contactCount": 2,
                "group": {
                    "id": 7,
                    "groupname": "group1",
                    "createdAt": "2017-11-16T10:53:12.000Z",
                    "updatedAt": "2017-11-16T10:53:12.000Z"
                }
            }
        ]
    },
    "alldata": [
        {
            "id": 7,
            "groupname": "group1",
            "createdAt": "2017-11-16T10:53:12.000Z",
            "updatedAt": "2017-11-16T10:53:12.000Z"
        },
        {
            "id": 8,
            "groupname": "group2",
            "createdAt": "2017-11-16T10:54:41.000Z",
            "updatedAt": "2017-11-16T10:54:41.000Z"
        }
    ]
}

我的api代码:

exports.getNewGroup = function (req, res) {
var globalObj={};
    ContactGroup.findAndCountAll({
        attributes: ['id', [sequelize.fn('COUNT', sequelize.col('group.id')),
            'contactCount'
        ]],
        include: [{
            model: Group,
        }],
    }).then(seenData => { globalObj.seendata=seenData;return Group.findAll();})
    .then(function (data) {
globalObj.alldata=data;
        return res.status(200).send(globalObj);
    }).catch(function (err) {
        return res.status(400).send(err.message);
    });
};

还有我的 Angular 客户端控制器代码:

$http({
        method: 'GET',
        url: '/api/getnewgroup'
      })
      .then(function (response) {
        $scope.groups = response.data.seendata.rows;
        $scope.alldata = response.data.alldata;
        console.log($scope.groups);
      }, function (response) {
        window.location.href = '/';
      });

还有我的 Jade 文件:

tr(ng-repeat='group in groups')
 td {{group.groupname}}
 td {{group.contactCount}}

我想显示 contactCount 值以及我想在我的前端显示 alldata.groupname 值?

帮帮我!

我得到了正确的 {{group.contactCount}},但我没有得到来自 alldata.groupname

的 {{group.groupname}}

如何获得?两个值以及如何传递多个范围并在 ng-repeat 中使用?

【问题讨论】:

  • 制作小提琴
  • 您需要将所有数据分配给 $scope。您只从响应中获取行。
  • 好的,我会接受,但是如何在里面传递两个范围数据?重复?您可以通过发布任何 sn-p 来帮助我吗?

标签: javascript mysql angularjs node.js sequelize.js


【解决方案1】:

最好将 allData 放入 $scope.alldata = response.data.alldata;

    $http({
      method: 'GET',
      url: '/api/getnewgroup'
    })
    .then(function (response) {
      $scope.groups = response.data.seendata.rows;
      $scope.allData = response.data.alldata;
      console.log($scope.groups);
    }, function (response) {
      window.location.href = '/';
    });

在你的 Jade 文件中

<tr ng-repeat='group in groups track by $index'>
  <td>
    <span ng-repeat='data in alldata'>data.groupname</span>
  </td>
  <td>group.contactCount</td>
</tr>

在 rows $index 的帮助下,您还可以获取 allData 的数据。希望它会工作

【讨论】:

  • 它不是 $scope.allData= response.data.seendata.alldata; $scope.allData=response.data.alldata;看看我的 json,它不工作兄弟,帮帮我
  • 使它成为 $scope.allData = response.data.alldata;
  • 我得到了位于 row: 内的contactCount 和 groupname,但我没有得到位于 alldata 内的 groupname :(
  • 调试过程首先打印 {{allData}} 是否像对象一样出现在屏幕上
  • in Jade step 1: td {{allData}} ---> 接下来的 step 2: print $index step 3: {{allData[$index]}} step 4: {{allData[$ index].groupname}}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-18
  • 1970-01-01
相关资源
最近更新 更多