AngularJs +查看/更新数据的最佳实践 - 更新后再次重新加载模型中的数据

时间:2014-10-08 19:16:09

标签: json angularjs

在我的angularjs应用程序中,我对标准的实现方式有点困惑。请查看我的代码结构并提出一些建议以使其更有效。

var app = angular.module("UserApp", []);

  app.controller('UsersListCtrl', function ($scope, $http) {
    $http.get('/ShowAllUsersList.json').success(function(data) {
      $scope.users = data;
    });
  });

  $http.get('/LoggedInUserInfo.json').
    success(function(data, status, headers, config) {
      $scope.logged_in_user = data;
    }).
    error(function(data, status, headers, config) {
      // log error
    });
  }    

  app.controller("LoggedInUserInfoCtrl", ['$scope', '$http', function($scope, $http) {
    $scope.updatePersonalInfo = function() {
      $scope.user_personal_info = {
        username: $scope.logged_in_user.username,
        primary_email: $scope.logged_in_user.primary_email,

        first_name: $('#first_name').val(),
        last_name: $('#last_name').val(),
        secondary_email: $('#secondary_email').val(),
      };

      $http.put('/UpdatePersonalInfo', $scope.user_personal_info).
        success(function(data, status, headers, config) {
           alert('Data updated successfully.');
        }).
        error(function(data, status, headers, config) {
          alert('error');
        });
    };

  }]);

现在我没有办法在重新加载页面时完全重新加载范围内的数据。我试图使用onload功能,但它无法正常工作。请提出建议。

提前致谢

1 个答案:

答案 0 :(得分:0)

您需要提供服务以跨控制器和范围持久保存数据。

请阅读文档:https://docs.angularjs.org/guide/services

还有视频:https://egghead.io/lessons/angularjs-sharing-data-between-controllers

相关问题