acking
V2EX  ›  问与答

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

  •  
  •   acking · Dec 16, 2016 · 1360 views
    This topic created in 3461 days ago, the information mentioned may be changed or developed.
    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 replies    2016-12-16 11:39:30 +08:00
    UnitTest
        1
    UnitTest  
       Dec 16, 2016
    每次都对同一个变量赋值,输出肯定就是最后一次赋值啊。
    forgcode
        2
    forgcode  
       Dec 16, 2016
    你的对象是同一个,只是引用!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5888 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 02:08 · PVG 10:08 · LAX 19:08 · JFK 22:08
    ♥ Do have faith in what you're doing.