0

另一个angularJS问题我有范围绑定一个点击,应该在第一次点击时添加一个值,在第二次点击时添加另一个值,但它只是不断返回和空数组并再次填充第一个值,为什么?:

scope.dbclickalert = function (scope, $watch) {
    var getCheckInDate = this.cellData.date;
    var formatcheckindate = new Date(getCheckInDate);
    var checkingdates = [];
    var curr_datecell = formatcheckindate.getDate();
    var padded_day = (curr_datecell < 10) ? '0' + curr_datecell : curr_datecell;
    var curr_monthcell = formatcheckindate.getMonth() + 1;
    var padded_month = (curr_monthcell < 10) ? '0' + curr_monthcell : curr_monthcell;
    var curr_yearcell = formatcheckindate.getFullYear();
    var date_stringcell =  + padded_month + "/" + padded_day + "/" + curr_yearcell;
    var checkindatestrg = "checkindate";
    console.log(checkingdates.length);
    if (checkingdates.length < 2) {
        alert("element exists in array");
        checkingdates.push('checkoutdate');
        checkingdates.push(date_stringcell);
        console.log(checkingdates + checkingdates.length);
    } else {
        checkingdates.push('checkindate');
        checkingdates.push(date_stringcell);
    }
    var setCheckInDate = el.hasClass('checkInDate');
    if (checkingdates === true) {
        alert('You have allready set your check In Date');
    } else {
        el.addClass('checkInDate');
        $('#checkoutbar').addClass('datePickerchecout');
    }
    $(".date-cell").each(function removeclasses() {
        $(this).removeClass('true');
    });
    return getCheckInDate;
};

好的,所以当我在函数之外声明它时,我得到未定义的错误:

   scope.checkingdates = [];
      scope.dbclickalert = function(scope, $watch){
         var getCheckInDate = this.cellData.date;
         var formatcheckindate = new Date(getCheckInDate);
         var checkingdates = scope.checkingdates;
         var curr_datecell = formatcheckindate.getDate();
         var padded_day = (curr_datecell < 10) ? '0'+curr_datecell : curr_datecell;
         var curr_monthcell = formatcheckindate.getMonth() + 1;
         var padded_month = (curr_monthcell < 10) ? '0'+curr_monthcell : curr_monthcell;
         var curr_yearcell = formatcheckindate.getFullYear();
         var date_stringcell = + padded_month + "/" + padded_day + "/" + curr_yearcell;
         var checkindatestrg = "checkindate";
         console.log(checkingdates.length);
           if (checkingdates.length < 2){
              alert("element exists in array");
              checkingdates.push('checkoutdate');
              checkingdates.push(date_stringcell);
              console.log(checkingdates+checkingdates.length);
          }else{
              checkingdates.push('checkindate');
              checkingdates.push(date_stringcell);
          }
         var setCheckInDate = el.hasClass('checkInDate');
          if (checkingdates === true){
              alert('You have allready set your check In Date');
          } else{
              el.addClass('checkInDate');
              $('#checkoutbar').addClass('datePickerchecout');
          }
             $(".date-cell").each(function removeclasses() {
                $(this).removeClass('true');
             });
              return getCheckInDate;
      };

好的,在第三个版本中,如果单击了相同的 div,则再次单击相同的数据“日期”,但如果单击了具有相同 ng-click="dbclickalert()" 的第二个 div,为什么?

link: function(scope, el, attributes, dateSheetCtrl, $watch) {
          scope.checkingdatesarry = [];
  scope.dbclickalert = function(){
                 var getCheckInDate = this.cellData.date;
                 var formatcheckindate = new Date(getCheckInDate);
                 var checkingdates = scope.checkingdates;
                 var curr_datecell = formatcheckindate.getDate();
                 var padded_day = (curr_datecell < 10) ? '0'+curr_datecell : curr_datecell;
                 var curr_monthcell = formatcheckindate.getMonth() + 1;
                 var padded_month = (curr_monthcell < 10) ? '0'+curr_monthcell : curr_monthcell;
                 var curr_yearcell = formatcheckindate.getFullYear();
                 var date_stringcell = + padded_month + "/" + padded_day + "/" + curr_yearcell;
                 var checkindatestrg = "checkindate";
                 var checkoutdatestrg = "checkoutdate";


                if( $.inArray('checkindate', scope.checkingdates) !== -1 )  {
                     scope.checkingdatesarry.push(checkoutdatestrg);
                      scope.checkingdatesarry.push(date_stringcell);
                      console.log(scope.checkingdatesarry + scope.checkingdatesarry.length);


                }
                else{
                scope.checkingdatesarry.push(checkindatestrg);
                      scope.checkingdatesarry.push(date_stringcell);
                      console.log(scope.checkingdatesarry + scope.checkingdatesarry.length);


                  }


                 var setCheckInDate = el.hasClass('checkInDate');
                  if (scope.checkingdates === true){
                      alert('You have allready set your check In Date');
                  } else{
                      el.addClass('checkInDate');
                      $('#checkoutbar').addClass('datePickerchecout');
                  }
                     $(".date-cell").each(function removeclasses() {
                        $(this).removeClass('true');
                     });
                     return  scope.checkingdatesarry;
  };

对于关心答案的任何人来说都可以,因为我的 div 是使用 angularJS 指令创建的,所以它返回每个 div 的数组而不是一个全局数组,我已经将数组从指令中取出并将其移动到服务中并且它工作正常。

4

1 回答 1

5

因为您在操作中使用空列表重新定义了数组dbclickalert。所以每次触发动作时,数组都会为空。

var checkingdates = [];

您应该将其移出函数并声明为

$scope.checkingdates = [];  //In your code, you may use scope.checkingdates = []; if you renamed the $scope when you inject it
于 2013-08-08T18:02:35.627 回答