【发布时间】:2026-02-12 00:20:06
【问题描述】:
如何将变量“$scope.dateformat”传递给“format” 指令。请 让我知道是否有任何示例可用或建议。 请给我推荐更多这方面的例子。
$scope.dateformat ="yyyy-mm-dd";
menuModule.directive("datepicker", function(){
return {
restrict: "EAC",
require: "ngModel",
scope: {
"ngModel": "="
},
link: function(scope, elem, attr){
$(elem).datepicker({
format: "yyyy-mm-dd", // Above $scope.dateformat should be
// called here
}).on("changeDate", function(e){
return scope.$apply(function(){
console.log(attr);
return scope.ngModel = e.format();
});
});
return scope.$watch("ngModel", function(newValue){
$(elem).datepicker("update", newValue);
});
}
};
});
【问题讨论】:
-
您将在链接函数的范围内获得日期格式。将 console.log(scope) 放入链接 fn 中,您将找到您的范围数据
-
那么这个 dateFormat 是在哪里声明的呢?它看起来在指令之外,是在指令所在的页面控制器中声明的吗?
-
日期格式在控制器内部,指令在控制器外部
-
第一个控制器应该加载,然后该指令应该加载
标签: jquery angularjs angularjs-directive