简体   繁体   English

如何在Angular js的控制器外部声明变量

[英]how to declare variable outside the controller in angular js

I want to declare a variable that I want to use in my two controllers. 我想声明一个要在我的两个控制器中使用的变量。 One controller is for posting values and the second controller is used for getting values from the db. 一个控制器用于发布值,第二个控制器用于从数据库获取值。 I have used a factory and it returns the time stamp. 我使用了一家工厂,它返回了时间戳。

This is my code: 这是我的代码:

mainApp.factory('methodFactory', function () {
    return { myMethod: function () {
            var date = new Date();
    //console.log(date);
var unique = ((date.getMonth()+1) + '' + date.getDate() + '' +  date.getFullYear() + ''+ date.getHours() +''+ date.getMinutes() +''+ date.getSeconds());
        return unique;
            //console.log("methodFactory - myMethod");
    }
}
});

When I use methodFactory () in my controllers, the values have changed. 当我在控制器中使用methodFactory ()时,值已更改。 Is there any way to have the same values in both of the controllers. 有没有办法在两个控制器中都具有相同的值。

It is normal since myMethod() function returns always a new Date(). 这是正常的,因为myMethod()函数始终返回新的Date()。 You don't store the information in the current object. 您不将信息存储在当前对象中。

Store the computed unique variable in a service and provide a getter and a setter to the value in this service. 将计算出的唯一变量存储在服务中,并为此服务中的值提供一个getter和setter。

here a simple example with two controllers where the first controller sets the value in the service and the second controller gets the value from the service: http://plnkr.co/edit/4WRtfVF3DnOfPDXz9mq0?p=preview 这里有两个控制器的简单示例,其中第一个控制器在服务中设置值,第二个控制器从服务中获取值: http : //plnkr.co/edit/4WRtfVF3DnOfPDXz9mq0?p=preview

js JS

var app = angular.module('plunker', []);

app.controller('MainCtrl', MainCtrl);
app.controller('SecondCtrl', SecondCtrl);



function MainCtrl($scope, sharedDataService) {

  $scope.setDate= function(){
    var date = new Date();
    var unique = ((date.getMonth()+1) + '' + date.getDate() + '' +  date.getFullYear() + ''+ date.getHours() +''+ date.getMinutes() +''+ date.getSeconds());
     sharedDataService.setDate(unique);
  }

}

function SecondCtrl($scope, sharedDataService) {

  $scope.getDate=function(){
    return sharedDataService.getDate();
  } 
}



app.service('sharedDataService', function() {
      var uniqueDate;

       this.getDate = function() {
        return uniqueDate;
      }

      this.setDate= function(newDate) {
        uniqueDate=newDate;
      }       
});

html HTML

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.5.x" src="https://code.angularjs.org/1.5.8/angular.js" data-semver="1.5.8"></script>
    <script src="app.js"></script>
  </head>

  <body>
    <div ng-controller="MainCtrl">
       <button ng-click="setDate()">submit</button>
    </div>
    <div ng-controller="SecondCtrl">
      <div>{{getDate()}}</div>
    </div>
  </body>

</html>
  • declare using a $rootScope or declare in a service 使用$ rootScope声明或在服务中声明

find the reference Global variables in AngularJS 在AngularJS中找到参考全局变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM