0

私はangularjsが初めてです。までのカウントダウンを示すディレクティブがありますendDate。ハードコードされた値ですべてが正常に機能します。

var endDate = new Date("2013-11-26T10:00:00Z");

しかし、ディレクティブには2つのユースケースがあるため、コントローラーまたはレンダリングされたテンプレートからディレクティブに値を動的に渡す方法を管理できません。

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>

助けてくれてありがとう。

4

1 に答える 1

0

スコープ経由で渡す必要があります。HTMLに含めることを検討してください:

<div your_directive 
    message="message">
</div>

ここで、メッセージはコントローラーで設定できるスコープ内の変数です。

$scope.message

次に、ディレクティブに次のように記述します。

app.directive("your_directive", function() {
return {
    restrict : "A",
    scope: {
        message: "="
    },

次に、次のようにリンク関数内でこの値にアクセスできます。

scope.message
于 2013-11-11T13:01:34.890 に答える