AngularJS,如何从指令中调用组件中的函数?

时间:2018-06-22 20:38:22

标签: angularjs angularjs-directive angularjs-scope

预先感谢,基本上在应用程序组件中有一个计时器,当我从指令调用时应启动该计时器,请让我知道如何调用?我尝试了一些方法,但没有帮助。我是angular js的新手,请引导我。谢谢这是Js File代码。

var app = angular.module('quizApp', []);
app.directive('quiz', function (quizFactory) {
  return {
    restrict: 'AE',
    scope: {},     
    templateUrl: '/Home/Dashboard',
    link: function (scope, elem, attrs) {

              //Calling function cmpTimer(); in Component
        };
     }
    }
});
 
app.component('cmpTimer', {
    bindings: {
        cmpFrom: '<',
        cmpUnit: '@'
    },
    template: '<div class="timer">{{$ctrl.timeRemaining}}</div>',
    controller: function ($interval) {
        var vm = this, ONE_SECOND = 1000, timerInterval;

        function startTimer() {
            var END_TIME = vm.getEndTime();

            function update() {
                vm.timeRemaining = moment((moment(END_TIME) -       moment())).format("HH:mm:ss");
            }

            timerInterval = $interval(function () {
                if (moment() > moment(END_TIME)) return vm.stopTimer();

                update();
            }, ONE_SECOND);

            update();
        }

        vm.getEndTime = function () {
            return moment().add(vm.cmpFrom, vm.cmpUnit);
        }

        vm.stopTimer = function () {
            alert('Time\'s up!');

            vm.$onDestroy();
        }

        vm.$onDestroy = function () {
            $interval.cancel(timerInterval);
        }

        vm.$onInit = startTimer;
    }
});

0 个答案:

没有答案