Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 在控制器中共享作用域数据_Angularjs - Fatal编程技术网

Angularjs 在控制器中共享作用域数据

Angularjs 在控制器中共享作用域数据,angularjs,Angularjs,我的SpringMVC控制器返回一个对象。 我的设想是: 单击一个页面上的按钮(如sample1.html)以表格的形式加载一个新页面(如sample2.html) 在带有button1和controller1-->的sample1.html中,单击button1-->后,我在controller1中获得了对象(假设是从后端获得的)。 但是在sample2.html中应该使用相同的对象来显示表 我们如何使用sample2.html中controller1中的这个对象 您可以使用服务存储数据,并将

我的SpringMVC控制器返回一个对象。 我的设想是: 单击一个页面上的按钮(如sample1.html)以表格的形式加载一个新页面(如sample2.html)

在带有button1和controller1-->的sample1.html中,单击button1-->后,我在controller1中获得了对象(假设是从后端获得的)。 但是在sample2.html中应该使用相同的对象来显示表


我们如何使用sample2.html中controller1中的这个对象

您可以使用服务存储数据,并将其注入控制器中。然后,当值更新时,您可以使用共享

以下是几个例子:

HTML视图

<div ng-controller="ControllerOne">
  CtrlOne <input ng-model="message">
  <button ng-click="handleClick(message);">LOG</button>
</div>

<div ng-controller="ControllerTwo">
  CtrlTwo <input ng-model="message">
</div>
服务

myModule.factory('mySharedService', function($rootScope) {
  var sharedService = {};

  sharedService.message = '';

  sharedService.prepForBroadcast = function(msg) {
    this.message = msg;
    this.broadcastItem();
  };

  sharedService.broadcastItem = function() {
    $rootScope.$broadcast('handleBroadcast');
  };
  return sharedService;
});

您可以使用
工厂
在控制器之间共享数据

    <div ng-controller="CtrlOne"> 

      <button ng-click="submit()">submit</button>
    </div>

    <div ng-controller="CtrlTwo">
       {{obj}}
    </div>

.controller('CtrlOne', function($scope, sampleFactory) {
      $scope.sampleObj = {
          'name': 'riz'
      }; //object u get from the backend
      $scope.submit = function() {
          sampleFactory.setObj($scope.sampleObj);
      }
  })
  .controller('CtrlTwo', function($scope, sampleFactory) {
      $scope.obj = sampleFactory.getObj();
  })
  .factory('sampleFactory', function() {
      var obj = {};
      return {
          setObj: function(_obj) {
              obj = _obj;
          },
          getObj: function() {
              return obj;
          }
      }
  })

提交
{{obj}}
.controller('CtrlOne',函数($scope,sampleFactory){
$scope.sampleObj={
“name”:“riz”
};//从后端获取的对象
$scope.submit=函数(){
sampleFactory.setObj($scope.sampleObj);
}
})
.controller('CtrlTwo',函数($scope,sampleFactory){
$scope.obj=sampleFactory.getObj();
})
.factory('sampleFactory',function(){
var obj={};
返回{
setObj:函数(_obj){
obj=_obj;
},
getObj:function(){
返回obj;
}
}
})

你能粘贴一些代码吗,这样我们就知道你到目前为止做了些什么?
    <div ng-controller="CtrlOne"> 

      <button ng-click="submit()">submit</button>
    </div>

    <div ng-controller="CtrlTwo">
       {{obj}}
    </div>

.controller('CtrlOne', function($scope, sampleFactory) {
      $scope.sampleObj = {
          'name': 'riz'
      }; //object u get from the backend
      $scope.submit = function() {
          sampleFactory.setObj($scope.sampleObj);
      }
  })
  .controller('CtrlTwo', function($scope, sampleFactory) {
      $scope.obj = sampleFactory.getObj();
  })
  .factory('sampleFactory', function() {
      var obj = {};
      return {
          setObj: function(_obj) {
              obj = _obj;
          },
          getObj: function() {
              return obj;
          }
      }
  })