我尝试使此脚本适用于 2 个不同的日期:
http://fiddle.jshell.net/yXjkq/1/
它适用于第一个,但不适用于第二个。我试图理解为什么?
JavaScript:
var mySecondModule = angular.module('MyReportModule', []);
function TheReportCtrl($scope) {
$scope.selectedDate = "Aladdin";
$scope.selectedDate2 = "Aladdin2";
}
mySecondModule.directive('datepicker', ['$parse', function($parse) {
return {
restrict: "A",
link: function(scope, element, attrs) {
//using $parse instead of scope[attrs.datepicker] for cases
//where attrs.datepicker is 'foo.bar.lol'
parsed = $parse(attrs.datepicker);
$(element).datepicker({
onSelect: function(dateText, inst) {
scope.$apply(function(){
parsed.assign(scope, dateText);
});
}
})
}
}
}]);
HTML:
<div ng-app="MyReportModule">
<div ng-controller="TheReportCtrl">
<input type="text" datepicker="selectedDate" />{{selectedDate}}
<input type="text" datepicker="selectedDate2" />{{selectedDate2}}
</div>
</div>