Angularjs范围不更新数据绑定ng-model textarea

时间:2015-05-11 03:58:24

标签: angularjs data-binding scope updating

我有问题,我已经做了一些功能来重置textarea,但它没有工作,我通过console.log跟踪范围正在更新但在textarea绑定由ng-model没有更新。看看代码是简单的代码

控制器中的

$scope.addJawaban = function(jawaban, x, noUrut, textarea) {
    $scope.jawaban = jawaban;
    $scope.jawab = jawaban;
    $scope.norut = x;
    $scope.jawabteks = textarea;
    console.log("jawaban = "+jawaban);
    $scope.isadd = true;
    $scope.istextarea = false;

    if (jawaban != '') {
      $scope.DisabledNext = false;
      // $scope.DisabledPrev = true;
    };

    if(jawaban == "..." || jawaban == "lainnya" || jawaban == "LAINNYA"){
      console.log("jawabannya adalaaaaaaah = " +jawaban +" itu adalaaaaaaah textarea");
      $scope.istextarea = true;
    }


    if (jawaban.split('|')[x] == "..." || jawaban.split('|')[x] == "lainnya" || jawaban.split('|')[x] == "LAINNYA") {
      $scope.radioquis = false;
      $scope.istextarea = true;
    }else{
      $scope.radioquis = true;

    }
    for (var i = 0; i < $scope.dataTracsaction.length; i++) {
      if ($scope.dataTracsaction[i].noUrut === noUrut && $scope.dataTracsaction[i].noIndex === ($scope.currentPage + 1)) {
        if (jawaban.split('|')[x] == "..." || jawaban.split('|')[x] == "lainnya" || jawaban.split('|')[x] == "LAINNYA") {
          // $scope.dataTracsaction[i].jawaban = textarea;
          // if (($scope.dataTracsaction[i].kodeJawab.split('|')[x]).length < 6) {
          //   $scope.dataTracsaction[i].kodeJawab = $scope.dataTracsaction[i].kodeJawab.replace($scope.dataTracsaction[i].kodeJawab.split('|')[x], $scope.dataTracsaction[i].kodeJawab.split('|')[x] + '#' + textarea);
          // };

        }else{
          // $scope.dataTracsaction[i].jawaban = jawaban.split('|')[x];
          // if (($scope.dataTracsaction[i].kodeJawab.split('|')[x]).length < 6) {
          //   $scope.dataTracsaction[i].kodeJawab = $scope.dataTracsaction[i].kodeJawab.replace($scope.dataTracsaction[i].kodeJawab.split('|')[x], $scope.dataTracsaction[i].kodeJawab.split('|')[x] + '#' + jawaban.split('|')[x]);
          // };
        }
      }

    }


  };

  $scope.historyData = [];
  $scope.ispush = true;
  $scope.lastPrev = false;

  $scope.next = function(x, z, noUrut, m) {


    console.log('x = '+ x);

    if ($scope.currentPage == $scope.dataTracsaction.length - 1) {
      console.log('true');
      // $scope.DisabledPrev = false;
      $scope.terakhir = true;
      $scope.lastPrev = true;
    }else{
      $scope.terakhir = false;
    }



      if ($scope.detik == 0) {
          $('#blmMulai').modal('toggle');
      }else{
          $scope.DisabledNext = true;

          if($scope.isadd){
            console.log('isadd = true');
            console.log('istextarea = '+$scope.istextarea);
            if($scope.istextarea == true || $scope.istextarea == 'true'){
              $scope.jawabsblm = $scope.jawabteks;
              console.log('push jawaban textarea = '+ $scope.jawabsblm);
            }else{
              $scope.jawabsblm = $scope.jawab.split('|')[$scope.norut];
              console.log('push jawaban  = '+ $scope.jawabsblm);

            }


          }


          for (var i = 0; i < $scope.historyData.length; i++) {
            if ($scope.historyData[i].prevPage == noUrut) {
              console.log('mendapatkan data hisory yg prevPage = ' +noUrut+' untuk mendapatkan nourut untuk split tujuan dan jawaban');
              if ($scope.isadd) {
                if (m.tujuan.split('|')[$scope.norut] == 99 || m.tujuan.split('|')[$scope.norut] == '99') {
                  $scope.historyData[i].thisPage = $scope.dataTracsaction.length;
                }else{
                  $scope.historyData[i].thisPage = m.tujuan.split('|')[$scope.norut];
                }
                $scope.historyData[i].indexjawab = $scope.norut;
                $scope.historyData[i].jawaban = $scope.jawabsblm;

                console.log("-------proses splice array------------");

                console.log("memulai splice index diatas " + i);
                 for (var j = 0; j < $scope.historyData.length; j++) {
                    if (j > i) {
                        $scope.historyData.splice(j, $scope.historyData.length);
                        console.log("menghapus index ke "+ j + " sampai "+  $scope.historyData.length +" dari historyData yang isinya =" + $scope.historyData[j]);
                    };
                };

                console.log("-------end proses splice array------------");

               console.log('update dilakukan');

              }else{
                $scope.norut = $scope.historyData[i].indexjawab;
              }
              $scope.ispush = false;
              console.log('indexjawab dari '+$scope.historyData[i].prevPage+' = '+$scope.norut);

            }
          };
          // $scope.DisabledPrev = false;

    if ($scope.terakhir != true) {
      if (m.tujuan.split('|')[$scope.norut] == 0 || m.tujuan.split('|')[$scope.norut] == '0' || m.tujuan.split('|')[$scope.norut] == null) {
        console.log('next kode_tanya = 0 currentPage next ke berikutnya');
        $scope.currentPage =  Number($scope.currentPage) + 2;
      }else if (m.tujuan.split('|')[$scope.norut] == 99 || m.tujuan.split('|')[$scope.norut] == '99') {
        console.log('next kode_tanya = 99 currentPage langsung next ke nomer terkahir');
        $scope.currentPage = $scope.dataTracsaction.length;
        console.log($scope.dataTracsaction.length);
      }else{
          $scope.currentPage = (m.tujuan.split('|')[$scope.norut]);

            };
    }

        if ($scope.ispush) {
          console.log("jawabsblm = " + $scope.jawabsblm);
          if ($scope.terakhir) {
            $scope.historyData.push({
               'prevPage': noUrut,
               'thisPage': $scope.dataTracsaction.length + 1,
               'jawaban' : $scope.jawabsblm,
               'indexjawab' : $scope.norut,
               'textarea' : $scope.istextarea 
            });

          }else{
             $scope.historyData.push({
               'prevPage': noUrut,
               'thisPage': Number($scope.currentPage),
               'jawaban' : $scope.jawabsblm,
               'indexjawab' : $scope.norut,
               'textarea' : $scope.istextarea 
            });
          }

          console.log('clear var x and jawabsblm and jawabteks');
        };


        for (var i = 0; i < $scope.dataTracsaction.length; i++) {
          if ($scope.dataTracsaction[i].noUrut === noUrut) {
            console.log("Dapet datat ransaksi berdasarkan Index = "+i+" lalu disampan");
              if ( $scope.jawaban.split('|')[$scope.norut] == "..." || $scope.jawaban.split('|')[$scope.norut] == "lainnya" || $scope.jawaban.split('|')[$scope.norut] == "LAINNYA" || $scope.istextarea) {
                $scope.dataTracsaction[i].kodeJawab = '';
                $scope.dataTracsaction[i].kodeJawab = m.kode_jawab.split('|')[$scope.norut] + '#' + $scope.jawabteks;
                $scope.jawabsblm = $scope.jawabteks;
                // $scope.istextarea = true;
              }else{
                if (x == '' || x == undefined || x == null) {
                  $scope.dataTracsaction[i].kodeJawab = '';
                  $scope.dataTracsaction[i].kodeJawab = m.kode_jawab.split('|')[$scope.norut] + '#' + $scope.jawaban.split('|')[$scope.norut];
                  $scope.jawabsblm = $scope.jawaban.split('|')[$scope.norut];
                  // $scope.istextarea = false;
                };


              }

              // console.log($scope.dataTracsaction[i].kodeJawab);

              console.log('jawab yang disampan = '+ $scope.dataTracsaction[i].kodeJawab);
          }
        }

        for (var k = 0; k < $scope.dataTracsaction.length; k++) {
          if ($scope.dataTracsaction[k].noUrut === noUrut && x !== undefined && x !== '') {
            // $scope.dataTracsaction[i].jawaban = x;
            // if (($scope.dataTracsaction[i].kodeJawab.split('|')[$scope.jawab]).length < 6) {
            //   $scope.dataTracsaction[i].kodeJawab = $scope.dataTracsaction[i].kodeJawab.replace($scope.dataTracsaction[i].kodeJawab.split('|')[$scope.jawab], $scope.dataTracsaction[i].kodeJawab.split('|')[$scope.jawab] + '#' + x);
            // }
              $scope.dataTracsaction[k].kodeJawab = '';
              $scope.dataTracsaction[k].kodeJawab = m.kode_jawab.split('|')[$scope.norut] + '#' + x;
            console.log('simpan jawaban textarea ='+ $scope.dataTracsaction[k].kodeJawab);
            $scope.jawabsblm = x;
            console.log('alone textarea');
          }
        }

        console.log('jawabsblm = '+ $scope.jawabsblm);



        x = '';
        $scope.jawab = '';
        $scope.jawaban = '';
        $scope.jawabteks = '';


        console.log('------------------------proses mencari history------------------------------');

        for (var i = 0; i < $scope.historyData.length; i++) {
          if ($scope.historyData[i].prevPage == $scope.currentPage) {
              console.log('mendapatkan data history yg prevPage = '+ $scope.currentPage +" lalu menampilkan jawabannya");

              $scope.indexHistory = i;
              $scope.istextarea = $scope.historyData[i].textarea;
              if ($scope.istextarea == false || $scope.istextarea == 'false') {
                $scope.jawab = $scope.historyData[i].jawaban;
                $scope.jawaban = $scope.historyData[i].jawaban;
              }else{
                $scope.jawabteks = $scope.historyData[i].jawaban;
              }

              console.log('jawaban dari '+$scope.historyData[i].prevPage+' = '+$scope.jawab+'atau ='+$scope.jawabteks);
              $scope.DisabledNext = false;
          }
        };

        console.log('------------------------selesai mencari history------------------------------');


        console.log($scope.historyData);



        console.log('noUrut = '+noUrut);

      if ($scope.terakhir != true) {
        for (var j= 0; j < $scope.dataTracsaction.length; j++) {
          if ($scope.dataTracsaction[j].noUrut == $scope.currentPage) {
            console.log(true);
            $scope.currentPage = j;
            break;

          };
        }
      }
        $scope.nourutsblm = noUrut;
        $scope.pagesblm = $scope.currentPage;

      }

    $scope.isadd = false;
    $scope.ispush = true;


    console.log('curPage= ' + $scope.currentPage);
    console.log('Trans.length= ' + $scope.dataTracsaction.length);

  };

  $scope.hapusJawaban = function(x, z, noUrut, ktanya){

    console.log('hapus');

    console.log('-------sebelum dihapus----------');
    console.log('jawab = '+$scope.jawab);
    console.log('jawabteks = '+$scope.jawabteks);

    console.log('-------------proses penghapusan---------------');
    $scope.jawab = "";
    $scope.jawabteks = "";
    $scope.DisabledNext = true;
    console.log('-------------end proses penghapusan---------------');

    console.log('-------sesudah dihapus----------');
    console.log('jawab = '+$scope.jawab);
    console.log('jawabteks = '+$scope.jawabteks);

    $scope.istextarea = false;

    console.log($scope.historyData);


  }

视野

 <div class="panel-body">
                    <form class="for-horizontal" role="form" style="margin-top:5px;">
                        <div ng-repeat="m in loadQuisioner.contents | startFrom:currentPage*pageSize | limitTo:pageSize | unique: 'nourut'" >
                            <div ng-hide="terakhir">
                                <div>
                                    {{m.nourut}}. &nbsp; {{m.pertanyaan}}
                                </div>
                                <div ng-show="m.multichoice == 1 && m.jawaban != '...'">
                                    <div ng-repeat="n in m.jawaban.split('|') track by $index">
                                        <input ng-model="jawab" ng-click="addJawaban(jawab, m.nourut, jawabteks)" type="checkbox" value="{{m.jawaban |split:'|':$index}}"> <label ng-if="(m.jawaban |split:'|':$index) != '...' && (m.jawaban |split:'|':$index) != 'lainnya' && (m.jawaban |split:'|':$index) != 'LAINNYA'" value="{{m.jawaban |split:'|':$index}}">{{m.jawaban |split:'|':$index + 1}}</label>
                                        <label ng-if="(m.jawaban |split:'|':$index) == '...' || (m.jawaban |split:'|':$index) == 'lainnya' || (m.jawaban |split:'|':$index) == 'LAINNYA'">LAINNYA(...)</label>
                                        <input type="hidden" ng-model="m.kode_tanya">
                                        <textarea class="form-control" ng-if="(m.jawaban |split:'|':$index) == '...' || (m.jawaban |split:'|':$index) == 'lainnya' || (m.jawaban |split:'|':$index) == 'LAINNYA'" ng-model="jawabteks" value="{{m.jawaban |split:'|':$index}}" ng-disabled="radioquis" name="jawabteks"></textarea>
                                    </div>
                                </div>
                                <div ng-show="m.multichoice == 0 && m.jawaban != '...'">
                                    <div ng-repeat="n in m.jawaban.split('|') track by $index">
                                        <input ng-model="jawab" type="radio" ng-click="addJawaban(m.jawaban,$index, m.nourut, jawabteks)" name="jawaban" value="{{m.jawaban |split:'|':$index}}"> <label ng-if="(m.jawaban |split:'|':$index) != '...' && (m.jawaban |split:'|':$index) != 'lainnya' && (m.jawaban |split:'|':$index) != 'LAINNYA'">{{m.jawaban |split:'|':$index}}</label>
                                        <label ng-if="(m.jawaban |split:'|':$index) == '...' || (m.jawaban |split:'|':$index) == 'lainnya' || (m.jawaban |split:'|':$index) == 'LAINNYA'">LAINNYA(...)</label>
                                        <input type="hidden" ng-model="m.kode_tanya" >
                                        <textarea class="form-control" ng-if="(m.jawaban |split:'|':$index) == '...' || (m.jawaban |split:'|':$index) == 'lainnya' || (m.jawaban |split:'|':$index) == 'LAINNYA'" ng-model="jawabteks" value="{{m.jawaban |split:'|':$index}}" ng-disabled="radioquis" name="jawabteks"></textarea>
                                    </div>
                                </div>
                                <div ng-show="m.jawaban == '...'">
                                    <textarea class="form-control" ng-model="jawabteks" ng-change="addJawaban(m.jawaban,$index, m.nourut, jawabteks)" name="jawabteks"></textarea>
                                </div>
                            </div>

                            <div ng-show="terakhir">
                                <div class="panel panel-info">
                                  <div class="panel-heading">Finish</div>
                                  <div class="panel-body">
                                    Silakan Anda tekan button submit untuk mensubmit kuesioner atau tekan Previous untuk mengedit Jawaban
                                    </br>
                                    </br>
                                    <button id="button" class="btn btn-default" ng-click="simpanQuisioner(jawabteks, m.nourut, m)">
                                        <span class="glyphicon glyphicon-ok-circle"></span> Submit
                                    </button>
                                  </div>
                                </div>
                            </div>
                            <div class="panel-footer" ng-show="loadQuisioner.contents.length">
                                <button class="btn btn-default" ng-disabled="currentPage == 0" ng-click="preview()">
                                    Previous
                                </button>
                                <button class="btn btn-default" ng-disabled="currentPage >= loadQuisioner.contents.length/pageSize || DisabledNext == true || terakhir == true" ng-click="next(jawabteks, $index, m.nourut, m)">
                                    Next
                                </button>
                                <button id="button" class="btn btn-default" ng-click="hapusJawaban(jawaban,m.jawaban, $index, m.kode_tanya)" ng-disabled="terakhir">
                                    <span class="glyphicon glyphicon-remove"></span> Hapus Jawaban
                                </button>
                               <!--  <button id="button" class="btn btn-default" ng-click="simpanQuisioner(jawab.Text, m.nourut, m)">
                                    <span class="glyphicon glyphicon-ok-circle"></span> Submit
                                </button> -->
                            </div>
                        </div>

                    </form>
                </div>

1 个答案:

答案 0 :(得分:0)

以下是清除textarea的简单工作示例:

Plunker

代码:

  $scope.reset = function() {
    $scope.myTextarea = '';
  }

HTML:

<textarea ng-model="myTextarea"></textarea>
<button ng-click="reset()">reset</button>

尝试将其剥离回刚重置textarea。看看它是否有用。