尝试切换具有启动和停止功能的按钮。
继承人
App.controller('LogPhoneMainController',
function LogPhoneMainController($scope) {
var self = this;
$scope.header = "Log a phone call";
$scope.stopwatches = [{ log: [] }, { interval: 1000, log: [] }, { interval: 2000, log: [] }];
});
App.filter('stopwatchTime', function () {
return function (input) {
if(input){
var elapsed = input.getTime();
var hours = parseInt(elapsed / 3600000,10);
elapsed %= 3600000;
var mins = parseInt(elapsed / 60000,10);
elapsed %= 60000;
var secs = parseInt(elapsed / 1000,10);
var ms = elapsed % 1000;
return hours + ':' + mins + ':' + secs + ':' + ms;
}
};
})
.directive('bbStopwatch', ['StopwatchFactory', function(StopwatchFactory){
return {
restrict: 'EA',
scope: true,
link: function(scope, elem, attrs){
var stopwatchService = new StopwatchFactory(scope[attrs.options]);
scope.startTimer = stopwatchService.startTimer;
scope.stopTimer = stopwatchService.stopTimer;
scope.resetTimer = stopwatchService.resetTimer;
}
};
}])
.factory('StopwatchFactory', ['$interval', function($interval){
return function(options){
var startTime = 0,
currentTime = null,
offset = 0,
interval = null,
self = this;
if(!options.interval){
options.interval = 100;
}
options.elapsedTime = new Date(0);
self.running = false;
function pushToLog(lap){
if(options.log !== undefined){
options.log.push(lap);
}
}
self.updateTime = function(){
currentTime = new Date().getTime();
var timeElapsed = offset + (currentTime - startTime);
options.elapsedTime.setTime(timeElapsed);
};
self.startTimer = function(){
if(self.running === false){
startTime = new Date().getTime();
interval = $interval(self.updateTime,options.interval);
self.running = true;
}
};
self.stopTimer = function(){
if( self.running === false) {
return;
}
self.updateTime();
offset = offset + currentTime - startTime;
pushToLog(currentTime - startTime);
$interval.cancel(interval);
self.running = false;
};
self.resetTimer = function(){
startTime = new Date().getTime();
options.elapsedTime.setTime(0);
timeElapsed = offset = 0;
};
self.cancelTimer = function(){
$interval.cancel(interval);
};
return self;
};
}]);
这是我的html。
<div class="container">
<div class="row">
<div ng-controller="LogPhoneMainController" ng-init="init()">
<h2>{{header}}</h2>
<div ng-repeat="options in stopwatches|limitTo:1">
<div bb-stopwatch options="options">
<div class="container">
<div class="stopWatch numbers">
{{options.elapsedTime | stopwatchTime}}
</div>
<button class="btn" ng-click="startTimer()">start</button>
<button class="btn" ng-click="stopTimer()">stop</button>
</div>
</div>
</div>
<div ng-view>
</div>
</div>
</div>
</div>
我只需要一个btn,并且ngclick需要切换它调用的函数并相应地显示iether开始或停止。
更新:
我试过了
$scope.startStop = "";
住在控制器中,下面是我的工厂
self.startStopTimer = function (startStop) {
if (startStop === "Start") {
startTimer();
$scope.startStop = "Stop";
} else {
stopTimer();
$scope.startStop = "Start";
}
};
我将代码放在控制器中,并且html按钮如下所示
<button class="btn" ng-click="startStopTimer()">
{{startStop}}
</button>
但是我没有定义范围(编辑:由于功能所在,我没有定义startStop)
最佳答案
在控制器中实现切换逻辑,并
使用控制器设置按钮文本。
<button class="btn" ng-click="startStopTimer()">{{startStop}}</button>
控制器中仅实现一项功能
startStopTimer
并根据
startStop
的值切换按钮中的文本。