V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
acking
V2EX  ›  问与答

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

  •  
  •   acking · 2016-12-16 11:27:23 +08:00 · 959 次点击
    这是一个创建于 2701 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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

    2 条回复    2016-12-16 11:39:30 +08:00
    UnitTest
        1
    UnitTest  
       2016-12-16 11:38:10 +08:00
    每次都对同一个变量赋值,输出肯定就是最后一次赋值啊。
    forgcode
        2
    forgcode  
       2016-12-16 11:39:30 +08:00
    你的对象是同一个,只是引用!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2129 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 16:15 · PVG 00:15 · LAX 09:15 · JFK 12:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.