dongqiang2024 2017-09-19 13:29
浏览 185
已采纳

选择ng-change导致所有选择字段自动更改

I know that the title is a bit confusing but I've done my best to describe it in a sentence. I want to use angular ng-change in select fields built with CodeIgniter. These selects are generated from an array. Everything is OK except these selects automatically select the same value for all select fields when I change one of them.

<?php $i=1; foreach ($data as $d) : ?>
Username: <input type="text" value="<?php echo $d->username ?>" /><br/>
Person: 
  <select ng-model="formData[<?php echo $i ?>].id" ng-change="updateUser()">
    <option value="1">Nina</option>
    <option value="2">Naila</option>
    <option value="3">Noni</option>
  </select><br/>
<?php $i++; endforeach; ?>

So when I change the select by choosing Nina in first select field, for example, then all select fields automatically selects option Nina.

UPDATE: In controller:

    angular.module('myApp')
.controller('userCtrl', function($scope,$http){

   $scope.formData = {};
   $scope.updateUser = function(){
      for (var i=1; i < $scope.formData.length; i++){
         console.log($scope.formData[i]);
      }
   }

With this, I got nothing when I examine it in console.

I tried to add attribute name=id[] on select but still no luck. Please help. Thanks.

  • 写回答

2条回答 默认 最新

  • dqm88684 2017-09-19 13:41
    关注

    I would avoid mixing php views together with AngularJS, but as you can't change that you can try making your model index based (Since as I mentioned in the comment, the problem here is using the same ngModel for the all select elements). Something like this could work:

    <?php $index = 0; ?>
    <?php foreach ($data as $d) : ?>
    Username: <input type="text" value="<?php echo $d->username ?>"/><br/>
    Person:
    <select ng-model="formData[<?php echo $index ?>].id" ng-change="updateUser()">
        <option value="1">Nina</option>
        <option value="2">Naila</option>
        <option value="3">Noni</option>
    </select><br/>
    <?php $index++; ?>
    <?php endforeach; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?