我尝试让这个脚本在两个不同的日期工作:
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>
答案 0 :(得分:1)
将此parsed = $parse(attrs.datepicker);
更改为var parsed = $parse(attrs.datepicker);
对我有用。请尝试告诉我们。