2013-08-08 1 views
0

Еще один вопрос с угловым вопросом. У меня есть область привязки клика, которая должна добавить одно значение для первого клика на другое значение при втором щелчке, но он просто сохраняет возвращаемый и пустой массив и снова заполняет первое значение. Почему? :Добавление существующего массива при щелчке просто пустет массив почему?

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()", почему?

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 вместо одного глобального массива я взял массиву весь путь из директивы и переместил его в службу, и он отлично работает.

ответ

5

Поскольку вы переопределили массив с пустым списком в действии dbclickalert. Поэтому каждый раз, когда действие запускается, массив будет пустым.

var checkingdates = []; 

Вы должны переместить его за пределы функции и объявить

$scope.checkingdates = []; //In your code, you may use scope.checkingdates = []; if you renamed the $scope when you inject it 
+0

да, но тогда, как я к нему доступ внутри dbclickalert? – vimes1984

+0

@ vimes1984 использовать scope.checkingdates. вы должны поддерживать переменные, добавив их в область $. – zsong

+0

Я получаю неопределенную ошибку, почему? – vimes1984

Смежные вопросы