Javascript 如何在工厂中移动控制器代码

Javascript 如何在工厂中移动控制器代码,javascript,angularjs,Javascript,Angularjs,我的控制器中有一些代码,但代码是可重用的。。所以我想把它搬到工厂里,然后在我需要它的时候使用它。。。我无法将此代码移到工厂。。如果我移动它,任何东西都不起作用了。这是我在控制器中的代码,我想在工厂中移动: var app = angular.module("clock.app"); app.controller('timer',['$scope','$interval','$timeout','timerFactory', function($scope, $interval,$time

我的控制器中有一些代码,但代码是可重用的。。所以我想把它搬到工厂里,然后在我需要它的时候使用它。。。我无法将此代码移到工厂。。如果我移动它,任何东西都不起作用了。这是我在控制器中的代码,我想在工厂中移动:

var app = angular.module("clock.app");
  app.controller('timer',['$scope','$interval','$timeout','timerFactory',
  function($scope, $interval,$timeout,timerFactory){
    var framework7App = new Framework7();
    var $$ = Dom7;
    $scope.timeList = [
      {"hour":0, "minutes":1, "seconds": 6},
      {"hour":0, "minutes":3, "seconds": 180},
      {"hour":0, "minutes":5, "seconds": 300}];

      var today = new Date();
      var arr,hour, minutes, seconds,convertedSec;

      var getStoredList = JSON.parse(localStorage.getItem("timeListDetails"));
      if(getStoredList !=null){
        if(getStoredList.length != 0){
            $scope.timeList = getStoredList;
        }else{
           localStorage.setItem("timeListDetails", JSON.stringify($scope.timeList));
        }
      }else{
          getStoredList = $scope.timeList;
      }
      $scope.timerWithInterval = 0;


      $scope.startTimerWithInterval = function() {
        $scope.timerWithInterval = 0;
        if($scope.myInterval){
          $interval.cancel($scope.myInterval);
        }
        $scope.onInterval = function(){
          $scope.timerWithInterval++;
        }
        $scope.myInterval = $interval($scope.onInterval,1000);
      };

      $scope.resetTimerWithInterval = function(){
        $scope.timerWithInterval = 0;
        $interval.cancel($scope.myInterval);
      }

      $scope.timeCounterInSeconds= function(seconds) {
        $scope.startTimerWithInterval();
        $timeout(function () {
          $scope.timeCounter(seconds)
        }, 1000);
      };

      $scope.timeCounter = function(seconds) {
        if($scope.timerWithInterval==seconds) {
          $scope.resetTimerWithInterval();
          framework7App.alert('Time Over','');
        }
        else {
          $timeout(function () {
            $scope.timeCounter(seconds)
          }, 1000);
        }
      };
      $scope.submit = function() {
        $scope.timeList.push({"hour":hour,
                              "minutes":minutes,
                              "seconds":seconds,
                              "convertedSec":convertedSec,
                              "timeFlag": true});
        localStorage.setItem("timeListDetails", JSON.stringify($scope.timeList));
        $scope.hidePopup();
      };

      $scope.displayPopup = function(){
        $scope.popupAddTimer = true;
      }
      $scope.hidePopup = function(){
        $scope.popupAddTimer = false;
      }

     timerFactory.picker();
}]);
我使用了以下工厂方法:

var factoryApp = angular.module('clock.factories');
    factoryApp.factory('timerFactory',[
        function() {

          var timerFactory = {};
          var framework7App = new Framework7();
          var $$ = Dom7;
          var today = new Date();
          var arr,hour, minutes, seconds,convertedSec;

            timerFactory.picker = function() {
                framework7App.picker({
                  input: '#picker-date',
                  container: '#picker-date-container',
                  toolbar: false,
                  rotateEffect: true,

                  value: [today.getHours(), (today.getMinutes() < 10 ? '0' + today.getMinutes() : today.getMinutes()), today.getSeconds()],
                  onOpen: function(p){
                  },
                  formatValue: function (p, values, displayValues) {
                    arr = displayValues[0] + ':' + values[1] + ':' +values[2];
                    hour = displayValues[0];
                    var  arrVal = arr.split(":");
                    convertedSec = (+arrVal[0] * 60 * 60 +(arrVal[1]) *60 +(+arrVal[2]));
                    minutes = values[1];
                    seconds = values[2];
                    return arr;
                  },
                  cols: [
                    // Hours
                    {
                      values: (function () {
                        var arr = [];
                        for (var i = 0; i <= 23; i++) { arr.push(i); }
                        return arr;
                      })(),
                    },
                    // Divider
                    {
                      divider: true,
                      content: ':'
                    },
                    // Minutes
                    {
                      values: (function () {
                        var arr = [];
                        for (var i = 0; i <= 59; i++) { arr.push(i < 10 ? '0' + i : i); }
                        return arr;
                      })(),
                    },
                    // Divider
                    {
                      divider: true,
                      content: ':'
                    },
                    // Seconds
                    {
                      values: (function () {
                        var arr = [];
                        for (var i = 0; i <= 59; i++) { arr.push(i < 10 ? '0' + i : i); }
                        return arr;
                    })(),
                  },
                ]
              });
                }
            return timerFactory;
        }]);
var factoryApp=angular.module('clock.factories');
factoryApp.factory('timerFactory'[
函数(){
var timerFactory={};
var framework7App=new Framework7();
var$$=Dom7;
var today=新日期();
var arr,小时,分钟,秒,转换秒;
timerFactory.picker=函数(){
framework7App.picker({
输入:“#选取者日期”,
容器:“#选取器日期容器”,
工具栏:false,
旋转效果:正确,
值:[today.getHours(),(today.getMinutes()<10?'0'+today.getMinutes():today.getMinutes()),today.getSeconds()],
onOpen:功能(p){
},
formatValue:函数(p、值、显示值){
arr=displayValues[0]+':'+值[1]+':'+值[2];
小时=显示值[0];
var arrVal=arr.split(“:”);
convertedSec=(+arrVal[0]*60*60+(arrVal[1])*60+(+arrVal[2]);
分钟=数值[1];
秒=数值[2];
返回arr;
},
科尔斯:[
//小时数
{
值:(函数(){
var-arr=[];
对于(var i=0;i
不,它也不允许$interval

要在工厂中使用
$interval
服务,只需将其插入工厂构造函数:

app.factory("timerFactory", function($interval) {
                     //inject here   ^^^^^^^^^^
    var timer = {};
    timer.count = 0;
    var intervalPromise;
    timer.start = function() {
        if (intervalPromise) return;
        intervalPromise = $interval(()=>(timer.count++), 1000);
    };
    timer.stop = function() {
        $interval.cancel(intervalPromise);
        intervalPromise = null;
    };
    return timer;
});

.

$interval在工厂中是可注射的;$scope在工厂中是不可注射的。@georgeawg no它也不允许$interval