从另一个控制器angularjs传递变量范围

时间:2015-01-03 10:35:22

标签: angularjs

我想将控制器中的范围变量传递给另一个控制器。我一直在尝试,但仍然没有工作。这是我的代码:

.factory('MyService',function(){
   return {varId_agen:''};
})
.controller('InputEditCtrl',function($scope,MyService){
   $scope.MyService=MyService;
   $scope.id_agen = $scope.MyService;
})
.controller('dataBusCtrl',function($scope,Buses,$timeout,TxData,$ionicModal,MyService){
   $scope.editData = function(dataBus){
      Service.varId_agen= dataBus.id_agen;
      $scope.editModal.show();
   };
})

1 个答案:

答案 0 :(得分:0)

U可以在服务中使用setter和getter方法来实现,请查看此代码     `

angular.module('myApp', []).factory('myService', function () {
var o = {x:1, y:2};
return {
    getO: getO,
    setO: setO
};

function getO() {
    return o;
}
function setO(x, y) {
    o.x = x;
    o.y = y;
}

});

angular.module('myApp').controller('MyCtrlA', function ($scope, myService) {
myService.setO(3, 4);
});

angular.module('myApp').controller('MyCtrlB', function ($scope, myService) {
var o = myService.getO(); // {x:3, y:4}
  });    `