ng-click没有在ng-repeat中触发?

时间:2014-01-14 17:22:06

标签: javascript angularjs

好的,我有以下内容:

    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()不会激活? 克里斯

1 个答案:

答案 0 :(得分:1)

主要问题是您应该将这些功能附加到选择项,而不是选项并使用ng-change

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

编辑:添加您的建议

<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}