angularjs - AngularJs如何获得 $http 结果

  显示原文与译文双语对照的内容

我有 Angular 写的模板但是我的编程风格跟不上。 下面是一些代码


(function () {
"use strict";
angular.module("app.tables", []).controller("tableCtrl", ["$scope","$filter","$http", '$window', function ($scope, $filter, $http, $window) {
 var init;
 $window.sessionStorage.setItem('token', 111);
 $http.post('/api/getAdminUsers', {}).success(function (data, status, headers, config) {
//$scope.stores = data;
 $scope.stores = data.users;

 }).error(function (data, status, headers, config) {
//called asynchronously if an error occurs
 alert(status);
 });
 return $scope.stores =[]
, $scope.searchKeywords ="", $scope.filteredStores = [], $scope.row ="", $scope.select = function (page) {
 var end, start;
 return start = (page - 1) * $scope.numPerPage, end = start + $scope.numPerPage, $scope.currentPageStores = $scope.filteredStores.slice(start, end)
 }, $scope.onFilterChange = function () {
 return $scope.select(1), $scope.currentPage = 1, $scope.row =""
 }, $scope.onNumPerPageChange = function () {
 return $scope.select(1), $scope.currentPage = 1
 }, $scope.onOrderChange = function () {
 return $scope.select(1), $scope.currentPage = 1
 }, $scope.search = function () {
 return $scope.filteredStores = $filter("filter")($scope.stores, $scope.searchKeywords), $scope.onFilterChange()
 }, $scope.order = function (rowName) {
 return $scope.row!== rowName? ($scope.row = rowName, $scope.filteredStores = $filter("orderBy")($scope.stores, rowName), $scope.onOrderChange()) : void 0
 }, $scope.numPerPageOpt = [3, 5, 10, 20], $scope.numPerPage = $scope.numPerPageOpt[2], $scope.currentPage = 1, $scope.currentPageStores = [], (init = function () {
 return $scope.search(), $scope.select($scope.currentPage)
 })()
}])

我想从 $http 获得数据,但是这个编程风格将所有的方法和变量返回给我的控制器。 我做了http请求,但它似乎没有将值返回给 $scope.stores 。 如何将。成功的侦听器放入返回,或者是否有它的他好的实用方法?

时间: 作者:

因为你的控制器不必返回接口。 接口被分配给 $scope 变量。


(function () {
"use strict";
angular.module("app.tables", [])
. controller("tableCtrl", ["$scope","$filter","$http",'$window', tableCtrl]; 

function tableCtrl($scope, $filter, $http, $window) {
 var init;
 $window.sessionStorage.setItem('token', 111);
 $http.post('/api/getAdminUsers', {})
. success(function (data, status, headers, config) {
//$scope.stores = data;
 $scope.stores = data.users;
 })
. error(function (data, status, headers, config) {
//called asynchronously if an error occurs
 alert(status);
 });
 $scope.stores = [];
 $scope.searchKeywords ="";
 $scope.filteredStores = [];
 $scope.row ="";
 $scope.select = function (page) {
 var end, start;
 return start = (page - 1) * $scope.numPerPage, end = start + $scope.numPerPage, $scope.currentPageStores = $scope.filteredStores.slice(start, end)
 };
 $scope.onFilterChange = function () {
 return $scope.select(1), $scope.currentPage = 1, $scope.row =""
 };
 $scope.onNumPerPageChange = function () {
 return $scope.select(1), $scope.currentPage = 1
 };
 $scope.onOrderChange = function () {
 return $scope.select(1), $scope.currentPage = 1
 };
 $scope.search = function () {
 return $scope.filteredStores = $filter("filter")($scope.stores, $scope.searchKeywords), 
 $scope.onFilterChange()
 };
 $scope.order = function (rowName) {
 return $scope.row!== rowName? ($scope.row = rowName, $scope.filteredStores = $filter("orderBy")($scope.stores, rowName), $scope.onOrderChange()) : void 0
 };
 $scope.numPerPageOpt = [3, 5, 10, 20];
 $scope.numPerPage = $scope.numPerPageOpt[2];
 $scope.currentPage = 1;
 $scope.currentPageStores = [];
 (init = function () {
 return $scope.search(), $scope.select($scope.currentPage)
 })()
})

关于你的代码有很多 to 。 但我还是要让它解决你目前的问题。

作者:
...