AngularJs如何使用一个控制器方法,在另一个控制器中使用变量

AngularJs如何使用一个控制器方法,在另一个控制器中使用变量,angularjs,Angularjs,我有两个控制器,如下所示。我想在其他控制器中使用第一个控制器方法/变量 app.controller("createController", ["$scope", function ($scope) { $scope.i = 0; $scope.changeEstimateStatus = function() { console.log('changeEstimateStatus'); }; }]); app.controller("editControll

我有两个控制器,如下所示。我想在其他控制器中使用第一个控制器方法/变量

app.controller("createController", ["$scope",
 function ($scope)
 {
  $scope.i = 0;
  $scope.changeEstimateStatus = function()
  {
    console.log('changeEstimateStatus');
  };
 }]);


 app.controller("editController", ["$scope",
 function ($scope)
 {
    //here how can I access 'i' variable of createController
 }]);
使用以下选项之一:

1.您可以使用serveice并将公共功能放入该服务,并将该功能访问给所有控制器

app.service('MyService', function() {
     this.changeEstimateStatus = function()
      {
        console.log('changeEstimateStatus');
      };
});

   app.controller("createController", ["$scope",MyService,
     function ($scope,MyService)
     {
      $scope.i = 0;
     MyService.changeEstimateStatus ();
     }]);


     app.controller("editController", ["$scope",   app.controller("createController", ["$scope",$rootScope,
     function ($scope,$rootScope)
     {
      $scope.i = 0;
      MyService.changeEstimateStatus ();

     }]);
2.您可以将该函数存储在$rootscope object.中,然后对所有Controller访问该函数

app.service('MyService', function() {
     this.changeEstimateStatus = function()
      {
        console.log('changeEstimateStatus');
      };
});

   app.controller("createController", ["$scope",MyService,
     function ($scope,MyService)
     {
      $scope.i = 0;
     MyService.changeEstimateStatus ();
     }]);


     app.controller("editController", ["$scope",   app.controller("createController", ["$scope",$rootScope,
     function ($scope,$rootScope)
     {
      $scope.i = 0;
      MyService.changeEstimateStatus ();

     }]);
比如:

但第二个选项不是一个好办法。

使用以下选项之一:

1.您可以使用serveice并将公共功能放入该服务,并将该功能访问给所有控制器

app.service('MyService', function() {
     this.changeEstimateStatus = function()
      {
        console.log('changeEstimateStatus');
      };
});

   app.controller("createController", ["$scope",MyService,
     function ($scope,MyService)
     {
      $scope.i = 0;
     MyService.changeEstimateStatus ();
     }]);


     app.controller("editController", ["$scope",   app.controller("createController", ["$scope",$rootScope,
     function ($scope,$rootScope)
     {
      $scope.i = 0;
      MyService.changeEstimateStatus ();

     }]);
2.您可以将该函数存储在$rootscope object.中,然后对所有Controller访问该函数

app.service('MyService', function() {
     this.changeEstimateStatus = function()
      {
        console.log('changeEstimateStatus');
      };
});

   app.controller("createController", ["$scope",MyService,
     function ($scope,MyService)
     {
      $scope.i = 0;
     MyService.changeEstimateStatus ();
     }]);


     app.controller("editController", ["$scope",   app.controller("createController", ["$scope",$rootScope,
     function ($scope,$rootScope)
     {
      $scope.i = 0;
      MyService.changeEstimateStatus ();

     }]);
比如:

但第二个选项不是一个好办法。

使用以下选项之一:

1.您可以使用serveice并将公共功能放入该服务,并将该功能访问给所有控制器

app.service('MyService', function() {
     this.changeEstimateStatus = function()
      {
        console.log('changeEstimateStatus');
      };
});

   app.controller("createController", ["$scope",MyService,
     function ($scope,MyService)
     {
      $scope.i = 0;
     MyService.changeEstimateStatus ();
     }]);


     app.controller("editController", ["$scope",   app.controller("createController", ["$scope",$rootScope,
     function ($scope,$rootScope)
     {
      $scope.i = 0;
      MyService.changeEstimateStatus ();

     }]);
2.您可以将该函数存储在$rootscope object.中,然后对所有Controller访问该函数

app.service('MyService', function() {
     this.changeEstimateStatus = function()
      {
        console.log('changeEstimateStatus');
      };
});

   app.controller("createController", ["$scope",MyService,
     function ($scope,MyService)
     {
      $scope.i = 0;
     MyService.changeEstimateStatus ();
     }]);


     app.controller("editController", ["$scope",   app.controller("createController", ["$scope",$rootScope,
     function ($scope,$rootScope)
     {
      $scope.i = 0;
      MyService.changeEstimateStatus ();

     }]);
比如:

但第二个选项不是一个好办法。

使用以下选项之一:

1.您可以使用serveice并将公共功能放入该服务,并将该功能访问给所有控制器

app.service('MyService', function() {
     this.changeEstimateStatus = function()
      {
        console.log('changeEstimateStatus');
      };
});

   app.controller("createController", ["$scope",MyService,
     function ($scope,MyService)
     {
      $scope.i = 0;
     MyService.changeEstimateStatus ();
     }]);


     app.controller("editController", ["$scope",   app.controller("createController", ["$scope",$rootScope,
     function ($scope,$rootScope)
     {
      $scope.i = 0;
      MyService.changeEstimateStatus ();

     }]);
2.您可以将该函数存储在$rootscope object.中,然后对所有Controller访问该函数

app.service('MyService', function() {
     this.changeEstimateStatus = function()
      {
        console.log('changeEstimateStatus');
      };
});

   app.controller("createController", ["$scope",MyService,
     function ($scope,MyService)
     {
      $scope.i = 0;
     MyService.changeEstimateStatus ();
     }]);


     app.controller("editController", ["$scope",   app.controller("createController", ["$scope",$rootScope,
     function ($scope,$rootScope)
     {
      $scope.i = 0;
      MyService.changeEstimateStatus ();

     }]);
比如:

但第二个选项不是一个好办法。

使用共享服务:

app.service("mySharedService", [function() {
    var _x = null;

    return {
        setX: function(val) { _x = val },
        getX: function() { return _x }
    }
}]);
然后将以下内容注入控制器:

app.controller("createController", ["$scope","mySharedService", function($scope, mySharedService) {

    $scope.i = mySharedService.getX(); //get
    mySharedService.setX(3); //set
}]);
使用共享服务:

app.service("mySharedService", [function() {
    var _x = null;

    return {
        setX: function(val) { _x = val },
        getX: function() { return _x }
    }
}]);
然后将以下内容注入控制器:

app.controller("createController", ["$scope","mySharedService", function($scope, mySharedService) {

    $scope.i = mySharedService.getX(); //get
    mySharedService.setX(3); //set
}]);
使用共享服务:

app.service("mySharedService", [function() {
    var _x = null;

    return {
        setX: function(val) { _x = val },
        getX: function() { return _x }
    }
}]);
然后将以下内容注入控制器:

app.controller("createController", ["$scope","mySharedService", function($scope, mySharedService) {

    $scope.i = mySharedService.getX(); //get
    mySharedService.setX(3); //set
}]);
使用共享服务:

app.service("mySharedService", [function() {
    var _x = null;

    return {
        setX: function(val) { _x = val },
        getX: function() { return _x }
    }
}]);
然后将以下内容注入控制器:

app.controller("createController", ["$scope","mySharedService", function($scope, mySharedService) {

    $scope.i = mySharedService.getX(); //get
    mySharedService.setX(3); //set
}]);

您应该将通用功能(changeEstimateStatus)移动到服务中。两个控制器都依赖于服务,而不是一个控制器依赖于另一个控制器

app.service('estimateService', function() {

  this.changeEstimateStatus = function()  {
    console.log('changeEstimateStatus');
  };

});
    app.controller("createController", ["$scope","estimateService",
 function ($scope, estimateService)
 {
  $scope.i = 0;
  $scope.changeEstimateStatus = function()
  {
    estimateService.changeEstimateStatus(); //delegate to the service
  };
 }]);


 app.controller("editController", ["$scope", "estimateService"
 function ($scope, estimateService)
 {
    $scope.changeEstimateStatus = function()
  {
    estimateService.changeEstimateStatus(); //delegate to the service
  };
 }]);

另一种选择是使用$rootScope,但使用服务并不像更改那样脆弱。

您应该将通用功能(changeEstimateStatus)移动到服务中。两个控制器都依赖于服务,而不是一个控制器依赖于另一个控制器

app.service('estimateService', function() {

  this.changeEstimateStatus = function()  {
    console.log('changeEstimateStatus');
  };

});
    app.controller("createController", ["$scope","estimateService",
 function ($scope, estimateService)
 {
  $scope.i = 0;
  $scope.changeEstimateStatus = function()
  {
    estimateService.changeEstimateStatus(); //delegate to the service
  };
 }]);


 app.controller("editController", ["$scope", "estimateService"
 function ($scope, estimateService)
 {
    $scope.changeEstimateStatus = function()
  {
    estimateService.changeEstimateStatus(); //delegate to the service
  };
 }]);

另一种选择是使用$rootScope,但使用服务并不像更改那样脆弱。

您应该将通用功能(changeEstimateStatus)移动到服务中。两个控制器都依赖于服务,而不是一个控制器依赖于另一个控制器

app.service('estimateService', function() {

  this.changeEstimateStatus = function()  {
    console.log('changeEstimateStatus');
  };

});
    app.controller("createController", ["$scope","estimateService",
 function ($scope, estimateService)
 {
  $scope.i = 0;
  $scope.changeEstimateStatus = function()
  {
    estimateService.changeEstimateStatus(); //delegate to the service
  };
 }]);


 app.controller("editController", ["$scope", "estimateService"
 function ($scope, estimateService)
 {
    $scope.changeEstimateStatus = function()
  {
    estimateService.changeEstimateStatus(); //delegate to the service
  };
 }]);

另一种选择是使用$rootScope,但使用服务并不像更改那样脆弱。

您应该将通用功能(changeEstimateStatus)移动到服务中。两个控制器都依赖于服务,而不是一个控制器依赖于另一个控制器

app.service('estimateService', function() {

  this.changeEstimateStatus = function()  {
    console.log('changeEstimateStatus');
  };

});
    app.controller("createController", ["$scope","estimateService",
 function ($scope, estimateService)
 {
  $scope.i = 0;
  $scope.changeEstimateStatus = function()
  {
    estimateService.changeEstimateStatus(); //delegate to the service
  };
 }]);


 app.controller("editController", ["$scope", "estimateService"
 function ($scope, estimateService)
 {
    $scope.changeEstimateStatus = function()
  {
    estimateService.changeEstimateStatus(); //delegate to the service
  };
 }]);


另一种选择是使用$rootScope,但使用服务并不是那么容易改变的。

控制器之间的关系如何?父/子-单独的页面?提供更多信息\似乎是使用带有变量i的服务,然后将其注入控制器单独页面的地方..请建议不使用$RootScope似乎是使用带有变量i的服务,然后将其注入控制器的地方-获取控制台错误控制器之间有何关联?父/子-单独的页面?提供更多信息\似乎是使用带有变量i的服务,然后将其注入控制器单独页面的地方..请建议不使用$RootScope似乎是使用带有变量i的服务,然后将其注入控制器的地方-获取控制台错误控制器之间有何关联?父/子-单独的页面?提供更多信息\似乎是使用带有变量i的服务,然后将其注入控制器单独页面的地方..请建议不使用$RootScope似乎是使用带有变量i的服务,然后将其注入控制器的地方-获取控制台错误控制器之间有何关联?父/子-单独的页面?提供更多信息\似乎是使用带有变量i的服务,然后将其注入控制器的地方分开的页面..请建议不使用$rootscope似乎是使用带有变量i的服务,然后将其注入控制器的地方-出现控制台错误我同意使用服务。不过,如果能知道原因,那就太好了。在我看来,最好使用服务,因为这样您就必须在需要的地方显式地导入依赖项。如果使用根作用域,该方法将在任何地方都可用。@BenCr服务始终是放置应在多个控制器之间共享的代码的正确位置,这正是OP想要的$应该尽可能避免使用rootScope,因为它与应用程序的每个部分共享,而服务只能在需要的地方注入。这不是我说的吗?我想说的一点是,只是说“这不是一个好方法”,而不告诉某人为什么不能获得高质量的答案。你永远不应该编写在任何地方都自动可用的方法,因为那样你可能会意外地被它们绊倒。这与不应该在JavaScript中使用全局变量的原因完全相同。我同意使用服务。不过,如果能知道原因,那就太好了。在我看来,最好使用服务,因为这样您就必须在需要的地方显式地导入依赖项。如果使用根作用域,该方法将在任何地方都可用。@BenCr服务始终是放置应在多个控制器之间共享的代码的正确位置,这正是OP想要的$应该尽可能避免使用rootScope,因为它与应用程序的每个部分共享,而服务只能在需要的地方注入。这不是我说的吗?我想说的一点是,只是说“这不是一个好方法”,而不告诉某人为什么不能获得高质量的答案。你永远不应该编写在任何地方都自动可用的方法,因为那样你可能会意外地被它们绊倒。这是完全一样的道理