여러 카운트 다운 타이머를 추적하는 각도 앱을 개발 중입니다. 사용자가 드롭 다운에서 새 값을 선택할 때 타이머를 다시 인스턴스화해야합니다. 나는 새 값을 선택할 때 메소드 호출이 일어나고 있다는 것을 확인하기위한 경고를 가지고 있지만, 타이머가 어떻게 다시 초기화되는지를 알지 못한다. http://siddii.github.io/angular-timer/AngularJS 타이머 다시 인스턴스화
apps.js :
나는이 타이머를 사용하고
var app = angular.module('TestTimer', ['timer']);
app.controller('OptionController', function() {
this.Options = [{name: 'name1',attr1: 10, attr2: 15, attr3: 8, attr4: 22},{name: 'name2',attr1: 45,attr2: 45, attr3: 15, attr4: 60
}];
this.selectedOption = {name: 'Selected Option',attr1: 30, attr2: 45, attr3: 15, attr4: 60};
this.isSelectedOption = function(name) {
return this.selectedOption.name === name;
};
this.getOptions = function() {
return this.Options;
};
this.getSelectedOption = function() {
return this.selectedOption;
};
this.setSelectedOption = function(Option) {
this.selectedOption = Option;
};
this.selectedTemplate = function() {
alert(this.selectedOption.name);
};
});
function TimerController($scope) {
$scope.timerRunning = false;
$scope.startTimer = function(){
$scope.$broadcast('timer-start');
$scope.timerRunning = true;
};
$scope.stopTimer = function(){
$scope.$broadcast('timer-stop');
$scope.timerRunning = false;
};
$scope.resetTimer = function() {
$scope.$broadcast('timer-reset');
}
$scope.$on('timer-stopped', function (event, data){
console.log('Timer Stopped - data = ', data);
});
$scope.$on('timer-tick', function(event, data) {
console.log('Timer ticked -', data);
});
}
TimerController.$inject = ['$scope'];
HTML :
이<body ng-app="TestTimer">
<div ng-controller="OptionController as main">
<h1>Test Timer</h1>
<h3>
<select ng-change='main.selectedTemplate()' ng-model="main.selectedOption" ng-class="form-control" ng-Options="Option.name for Option in main.Options" title="Options">
<Option value="">Pick a Option</Option>
</select>
</h3>
<div ng-controller="TimerController as timer">
<h3>attr1: <timer autostart="false" countdown="main.selectedOption.attr1" interval="1000" finish-callback="startTimer()">{{countdown}}</timer></h3>
<h3>attr2: <timer autostart="false" countdown="main.selectedOption.attr2" interval="1000" finish-callback="startTimer()">{{countdown}}
</timer></h3>
<h3>attr3: <timer autostart="false" countdown="main.selectedOption.attr3" interval="1000" finish-callback="startTimer()">{{countdown}}</timer></h3>
<h3>attr4 Launcher: <timer autostart="false" countdown="main.selectedOption.attr4" interval="1000" finish-callback="startTimer()">{{countdown}}</timer></h3>
<button ng-click="startTimer()" ng-disabled="timerRunning">Start Timer</button>
<button ng-click="stopTimer()" ng-disabled="!timerRunning">Stop Timer</button>
</div>
attr-name: {{ main.selectedOption.name }} <br />
attr1: {{ main.selectedOption.attr1 }} <br />
attr2: {{ main.selectedOption.attr2 }} <br />
attr3: {{ main.selectedOption.attr3 }} <br />
attr4: {{ main.selectedOption.attr4 }} <br />
</div>
</body>
편집 : plunkr이 - http://plnkr.co/edit/TADnlLBdpDI0mxJuq23A?p=preview
내가 각 타이머 지시어의 사용자 정의 구현을했다. 제발 [여기보세요] (http://stackoverflow.com/questions/26450240/does-angularjs-help-out-in-any-way-when-it-comes-to-have-a-duration-counter-on- m) 및 [여기] (http://stackoverflow.com/questions/26775808/angular-timer-directive-not-working-with-ionic-framework) 각도 타이머는 jquery 기반, 따라서 나는 그것을 좋아하지 않는다. 많은 – harishr