我是angularjs的新手。我有一个指示显示endDate
的倒计时。一切都运行良好,硬编码值:
var endDate = new Date("2013-11-26T10:00:00Z");
但我无法管理如何动态地从控制器或渲染模板将值传递给指令,因为我的指令有两个用例。
CountDownDirective.js:
app.directive("countdown", ["$window", function($window) {
return {
restrict: "EA",
replace: true,
link: function(scope, element, attrs, controller) {
function countdown(){
var endDate = new Date("2013-11-26T10:00:00Z"); //hardcode
//some logic...
}
countdown();
}
}
}]);
SomeController.js:
app.controller("SomeController", ["$scope", '$rootScope', "APIService",
function($scope, $rootScope, APIService){
APIService.get('end_data').then(function(response){
$scope.end_data = response;
// response is "2013-11-26T10:00:00Z"
});
//some logic...
}
]);
template_one.html:
<div ng-controller="SomeController">
<div countdown>...</div>
</div>
template_two.html:
<div countdown end-date="2013-11-26T10:00:00Z">...</div>
提前感谢您的帮助。
答案 0 :(得分:0)
你应该通过范围传递它。考虑使用html:
<div your_directive
message="message">
</div>
其中message在范围内是可变的,您可以通过控制器设置:
$scope.message
然后你会在你的指令中:
app.directive("your_directive", function() {
return {
restrict : "A",
scope: {
message: "="
},
然后您可以在链接函数中访问此值,如下所示:
scope.message