NG-点击NG重复不触发? [英] ng-click not firing in ng-repeat?

查看:81
本文介绍了NG-点击NG重复不触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有以下几点:

    scope.monthpickerclick = function(scope){
    $window.alert('test'); 
        console.log(scope);
    };
scope.monthpicker = function(alldatesdumparray){

var alldatesdump = booking.getalldates();
var alldatesdumparray = $.map(booking.getalldates(), function(value, index) {
    var dropdates = new Date(value.date);
    var dropdate = dropdates.getDate();
    var month=new Array();
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";
    var dropmonth = month[dropdates.getMonth()];
    var checkmonth = dropdates.getMonth();
    var dropyear = dropdates.getFullYear();
    var joindates = dropmonth + '-' + dropyear;
    var monthyear = checkmonth + '- '
    var today = new Date();
    var mm = today.getMonth(); //January is 0!
    var yyyy = today.getFullYear();

    if(mm < checkmonth && dropyear < yyyy){

    }else{
        value.date = joindates;
        value.month = checkmonth;
        value.Year =  dropyear;
        return [value];
    }
});
//console.log(alldatesdumparray);
    var dupes = {};
    var singles = [];
$.each(alldatesdumparray, function(i, el) {
    if (!dupes[el.date]) {
        dupes[el.date] = true;
        singles.push(el);
        return singles;
    }
});
return singles;
};

HTML

<select >
<option ng-click="monthpickerclick()" class="animate-repeat" ng-repeat="returnpicker in monthpicker(singles)" value="{{returnpicker.date}}">{{returnpicker.date}}</option>
</select>

它输出:

<select>
    <!-- ngRepeat: returnpicker in monthpicker(singles) -->
    <option ng-click="monthpickerclick()" class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(singles)" value="January-2014">January-2014</option>
    <option ng-click="monthpickerclick()" class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(singles)" value="February-2014">February-2014</option>
    <option ng-click="monthpickerclick()" class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(singles)" value="March-2014">March-2014</option>
    <option ng-click="monthpickerclick()" class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(singles)" value="April-2014">April-2014</option>
    <option ng-click="monthpickerclick()" class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(singles)" value="May-2014">May-2014</option>
    <option ng-click="monthpickerclick()" class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(singles)" value="June-2014">June-2014</option>
    <option ng-click="monthpickerclick()" class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(singles)" value="July-2014">July-2014</option>
    <option ng-click="monthpickerclick()" class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(singles)" value="August-2014">August-2014</option>
    <option ng-click="monthpickerclick()" class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(singles)" value="September-2014">September-2014</option>
    <option ng-click="monthpickerclick()" class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(singles)" value="October-2014">October-2014</option>
    <option ng-click="monthpickerclick()" class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(singles)" value="November-2014">November-2014</option>
    <option ng-click="monthpickerclick()" class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(singles)" value="December-2014">December-2014</option>
</select>

这看起来不错...
那么,为什么monthpickerclick()不是当我选择一个选项火?
克里斯

which looks fine... SO why does monthpickerclick() not fire when i'm selecting an option? Chris

推荐答案

的主要问题是,你应该重视这些功能来选择项目,而不是选择和使用NG-变化

The main issue is that you should attach these functions to the select item, rather than the option and use ng-change

<select ng-change="monthPickerClick()" >
    <option class="animate-repeat" ng-repeat="returnpicker in monthpicker(singles)" value="{{returnpicker.date}}">{{returnpicker.date}}</option>
</select>

编辑:添加在您的建议

Adding in your suggestion

<select ng-change="monthPickerClick()" ng-model="myItem">
    <option class="animate-repeat" ng-repeat="returnpicker in monthpicker(singles)" value="{{returnpicker.date}}">{{returnpicker.date}}</option>
</select>

$scope.$watch('myItem', function(){
    //do something
});

第二个编辑:

<select ng-change="monthPickerClick()" ng-model="myItem" ng-init="savedReturnPicker=[]">
    <option class="animate-repeat" model="savedReturnPicker[returnpicker.date]" ng-repeat="returnpicker in monthpicker(singles)" value="{{returnpicker}}">{{returnpicker.date}}</option>
</select>

//save the returnPicker to a model called savedReturnPicker, then look up with $scope.savedReturnPicker[someDateGoesHere}

这篇关于NG-点击NG重复不触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆