【问题标题】:How to write a function that is common for all the Controllers如何编写所有控制器通用的函数
【发布时间】:2018-02-20 22:03:43
【问题描述】:

我正在开发一个微型金融 Angular 应用程序,因为我正在尝试定位登录到我的应用程序的代理的位置...并将详细信息存储在集合中(ActivityDetails)...但是要求完全不同。 ..就像一旦代理登录到我们的应用程序意味着每隔一小时就应该跟踪位置并将其存储在集合(ActivityDetails)中......为此我使用了设置间隔功能......它成功执行但是当我导航到不同的控制器该功能没有执行,因为我在登录控制器中编写位置跟踪和存储代码,当它导航到差异时..控制器我面临问题......我怎样才能使这些功能在登录后成功运行 并且无论任何控制器..请帮助我找到解决方案..我是Angular的新手,我有3个月的经验...下面我正在添加功能..请验证并指导我。 .谢谢你..

///////Signin Controller code for location tracking//////////

     $scope.nearme = function(firstname,activity,date,time) {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (position) 
          {
            lat = position.coords.latitude; 
            lng = position.coords.longitude;
            var latlng = new google.maps.LatLng(lat, lng);
            var geocoder = geocoder = new google.maps.Geocoder();
            geocoder.geocode({ 'latLng': latlng }, function (results, status) 
                {
                if (status == google.maps.GeocoderStatus.OK) 
                {
                    if (results[1]) 
                    {
                      console.log(results[1].formatted_address)
                      console.log(firstname)
                      console.log(date)
                      console.log(time);
                      var obj={};
                      obj["User"]=firstname;
                      obj["Activity"]=activity;
                      obj["Date"]=$scope.date;
                      obj["Time"]=$scope.time;
                      obj["LocationDetails"]=results[1].formatted_address;

                      console.log(obj)
                   $http.post("/ncrActivityDetails",obj).success(function(res9){
                        console.log(res9)
                      })
                    }
                }
            });

        });  
    }
}
/////////////setInterval Function for calling every one hour///////////

    setInterval(function(){
    $scope.date3 = {date1:new Date()}
    $scope.activity="Tracking";
     var  dates  = new Date(((new Date($scope.date3.date1).toISOString().slice(0, 23))+"-05:30")).toISOString();
            var a = dates.split("T");
            $scope.date = a[0];
            $scope.time = a[1];
            console.log($scope.date)
            console.log($scope.time)
    $scope.nearme($scope.firstname,$scope.activity,$scope.date,$scope.time);
},3600000);

////////////////ActivityDetails collection sample data/////////

    {
    "_id" : ObjectId("5a8a6936fa642a14149a12d1"),
    "User" : "7829780519",
    "Activity" : "Login",
    "Date" : "2018-02-19",
    "Time" : "11:35:26.777Z",
    "LocationDetails" : "Sri laxmivenkateshwara, #1/10, 4th Main, Srinivasanagar, B S K 3rd Stage, Water Tank Rd, Banashankari 3rd Stage, Banashankari, Bengaluru, Karnataka 560085, India"
}

【问题讨论】:

  • 为此编写一个服务/工厂。将您的函数作为this.nearme = function(firstname,activity,date,time){...} 转移到那里,并在您的控制器中使用YourService.nearme(...) 调用它。 (另外,您在函数中使用$scope.date,而您将date 作为参数传递)

标签: angularjs


【解决方案1】:

解决方案:1) 在你的 .run 模块中创建你的函数,这样即使你已经导航到不同的控制器它也能工作, check this link

.run(function($interval){
   setInterval(function(){
$rootScope.nearme($scope.firstname,$scope.activity,$scope.date,$scope.time);},3600000);});

2) 创建服务 - link

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-08-15
  • 1970-01-01
  • 1970-01-01
  • 2011-01-09
  • 2010-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多