请问这段 angularjs 代码该怎么写?

2016-12-16 11:27:23 +08:00
 acking
if($scope.users){
    angular.forEach($scope.users,function(v){
          if(v.sel===true){
              $scope.messageModel.uid = v.u_id
              $scope.msg.push($scope.messageModel);
         }
    })
 }
console.log($scope.msg);

其中$scope.messageModel 是一个对象。 打印出来 uid 应该不相同。但是却取的是最后一个 v.uid

970 次点击
所在节点    问与答
2 条回复
UnitTest
2016-12-16 11:38:10 +08:00
每次都对同一个变量赋值,输出肯定就是最后一次赋值啊。
forgcode
2016-12-16 11:39:30 +08:00
你的对象是同一个,只是引用!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/328034

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX