Javascript 如何在factory AngularJS中使用API中的数据

Javascript 如何在factory AngularJS中使用API中的数据,javascript,html,angularjs,angularjs-service,Javascript,Html,Angularjs,Angularjs Service,我有下面的代码,它使用staff数组中的数据来计算工作人员的薪酬 “严格使用”; var-app=angular.module('app',[]); app.factory('staffFactory',函数($http){ var职员=[ {“id”:“1”,“name”:“Kate”,“rate”:“10”,“hours”:“10”}, {“id”:“2”,“name”:“John”,“rate”:“20”,“hours”:“10”}, {“id”:“3”,“name”:“Matt”,“r

我有下面的代码,它使用staff数组中的数据来计算工作人员的薪酬

“严格使用”;
var-app=angular.module('app',[]);
app.factory('staffFactory',函数($http){
var职员=[
{“id”:“1”,“name”:“Kate”,“rate”:“10”,“hours”:“10”},
{“id”:“2”,“name”:“John”,“rate”:“20”,“hours”:“10”},
{“id”:“3”,“name”:“Matt”,“rate”:“15”,“hours”:“10”}
];
函数calcpayiner(){
var unique={},
独特=[],支付=[];
对于(员工中的var i){
if(typeof(unique[staff[i].id])==“未定义”){
distinct.push(staff[i].id);
}
unique[staff[i].id]=unique[staff[i].id]|{pay:0};
唯一的[staff[i].id]。名称=staff[i]。名称;
唯一的[staff[i].id].pay+=(parseInt(staff[i].rate,10)*parseInt(staff[i].hours,10));
}
for(唯一中的var p){
pay.push([p,unique[p]]);
pay.sort(功能(a、b){
返回(b[1]。支付-a[1]。支付);
});
}
报酬;
}
var staffService={};
staffService.allStaff=函数(){
返回工作人员;
};
staffService.CalcPay=函数(){
返回calcpayiner();
};
返回员工服务;
});
app.controller('MainCtrl',['$scope','staffFactory',函数($scope,staffFactory){
$scope.staff=staffFactory.allStaff();
console.log($scope.staff);
$scope.CalcPay=staffFactory.CalcPay();
$scope.keyPress=函数(keyCode){
$scope.CalcPay=staffFactory.CalcPay();
};    
}]);

命名时间
{{s.name}}
名牌
{{b.1.name}
{{b.1.支付}
我就是这样做的:

$scope.documents = [];

$http.post('get_xml.php')
    .then(function (result) {
        $scope.documents = result.data;
        console.log(result.data);
    });
据我所知,
。然后,
是一个异步函数,它将在阵列可用时立即更新阵列

因此,在您的情况下,应该是:

app.factory('staffFactory', function ($http) {

    var staff = [];
    var test = $http.get('staff.json').then(function(result) {
        staff = result.data;
        console.log(staff);
    });

    /* Rest of your code... */
});
我就是这样做的:

$scope.documents = [];

$http.post('get_xml.php')
    .then(function (result) {
        $scope.documents = result.data;
        console.log(result.data);
    });
据我所知,
。然后,
是一个异步函数,它将在阵列可用时立即更新阵列

因此,在您的情况下,应该是:

app.factory('staffFactory', function ($http) {

    var staff = [];
    var test = $http.get('staff.json').then(function(result) {
        staff = result.data;
        console.log(staff);
    });

    /* Rest of your code... */
});
我就是这样做的:

$scope.documents = [];

$http.post('get_xml.php')
    .then(function (result) {
        $scope.documents = result.data;
        console.log(result.data);
    });
据我所知,
。然后,
是一个异步函数,它将在阵列可用时立即更新阵列

因此,在您的情况下,应该是:

app.factory('staffFactory', function ($http) {

    var staff = [];
    var test = $http.get('staff.json').then(function(result) {
        staff = result.data;
        console.log(staff);
    });

    /* Rest of your code... */
});
我就是这样做的:

$scope.documents = [];

$http.post('get_xml.php')
    .then(function (result) {
        $scope.documents = result.data;
        console.log(result.data);
    });
据我所知,
。然后,
是一个异步函数,它将在阵列可用时立即更新阵列

因此,在您的情况下,应该是:

app.factory('staffFactory', function ($http) {

    var staff = [];
    var test = $http.get('staff.json').then(function(result) {
        staff = result.data;
        console.log(staff);
    });

    /* Rest of your code... */
});

其他人提到的问题是,您试图在数据可用之前获取数据。解决这个问题的办法是,如果你想保持数据的单一来源,就在你的工厂里使用承诺

下面的代码使用
q
,因此您必须将其注入工厂

app.factory('staffFactory', function ($http, $q) {}
全员执行

/* Private var to hold the staff */
var staff = [];

// this method returns a promise
staffService.allStaff = function () {
    var deferred = $q.defer();

    // check if staff is already fetched from server if yes resolve the promise immediately
    if (staff.length > 0) {
        // we have data already. we can avoid going to server
        deferred.resolve(staff);   // staff holds all the data
    } else {
        // data is not available yet. fetch it from server

        $http.get('staff.json')
        .success(function(data) {
            // once data is available resolve
            staff = data;
            deferred.resolve(data);
        })
        .error(function (error) {
            deferred.reject(error);
        });
    }

    return deferred.promise;
};
在控制器中

staffFactory.allStaff().then(function (staff) {
    $scope.staff = staff;
    console.log($scope.staff);
    $scope.CalcPay = staffFactory.CalcPay();
    $scope.keyPress = function(keyCode) {
        $scope.CalcPay = staffFactory.CalcPay();
    }; 
});

签出

其他人提到的问题是,您试图在数据可用之前获取数据。解决这个问题的办法是,如果你想保持数据的单一来源,就在你的工厂里使用承诺

下面的代码使用
q
,因此您必须将其注入工厂

app.factory('staffFactory', function ($http, $q) {}
全员执行

/* Private var to hold the staff */
var staff = [];

// this method returns a promise
staffService.allStaff = function () {
    var deferred = $q.defer();

    // check if staff is already fetched from server if yes resolve the promise immediately
    if (staff.length > 0) {
        // we have data already. we can avoid going to server
        deferred.resolve(staff);   // staff holds all the data
    } else {
        // data is not available yet. fetch it from server

        $http.get('staff.json')
        .success(function(data) {
            // once data is available resolve
            staff = data;
            deferred.resolve(data);
        })
        .error(function (error) {
            deferred.reject(error);
        });
    }

    return deferred.promise;
};
在控制器中

staffFactory.allStaff().then(function (staff) {
    $scope.staff = staff;
    console.log($scope.staff);
    $scope.CalcPay = staffFactory.CalcPay();
    $scope.keyPress = function(keyCode) {
        $scope.CalcPay = staffFactory.CalcPay();
    }; 
});

签出

其他人提到的问题是,您试图在数据可用之前获取数据。解决这个问题的办法是,如果你想保持数据的单一来源,就在你的工厂里使用承诺

下面的代码使用
q
,因此您必须将其注入工厂

app.factory('staffFactory', function ($http, $q) {}
全员执行

/* Private var to hold the staff */
var staff = [];

// this method returns a promise
staffService.allStaff = function () {
    var deferred = $q.defer();

    // check if staff is already fetched from server if yes resolve the promise immediately
    if (staff.length > 0) {
        // we have data already. we can avoid going to server
        deferred.resolve(staff);   // staff holds all the data
    } else {
        // data is not available yet. fetch it from server

        $http.get('staff.json')
        .success(function(data) {
            // once data is available resolve
            staff = data;
            deferred.resolve(data);
        })
        .error(function (error) {
            deferred.reject(error);
        });
    }

    return deferred.promise;
};
在控制器中

staffFactory.allStaff().then(function (staff) {
    $scope.staff = staff;
    console.log($scope.staff);
    $scope.CalcPay = staffFactory.CalcPay();
    $scope.keyPress = function(keyCode) {
        $scope.CalcPay = staffFactory.CalcPay();
    }; 
});

签出

其他人提到的问题是,您试图在数据可用之前获取数据。解决这个问题的办法是,如果你想保持数据的单一来源,就在你的工厂里使用承诺

下面的代码使用
q
,因此您必须将其注入工厂

app.factory('staffFactory', function ($http, $q) {}
全员执行

/* Private var to hold the staff */
var staff = [];

// this method returns a promise
staffService.allStaff = function () {
    var deferred = $q.defer();

    // check if staff is already fetched from server if yes resolve the promise immediately
    if (staff.length > 0) {
        // we have data already. we can avoid going to server
        deferred.resolve(staff);   // staff holds all the data
    } else {
        // data is not available yet. fetch it from server

        $http.get('staff.json')
        .success(function(data) {
            // once data is available resolve
            staff = data;
            deferred.resolve(data);
        })
        .error(function (error) {
            deferred.reject(error);
        });
    }

    return deferred.promise;
};
在控制器中

staffFactory.allStaff().then(function (staff) {
    $scope.staff = staff;
    console.log($scope.staff);
    $scope.CalcPay = staffFactory.CalcPay();
    $scope.keyPress = function(keyCode) {
        $scope.CalcPay = staffFactory.CalcPay();
    }; 
});

签出

谢谢。我读到的每一件事都表明,为了避免控制器出现所有业务逻辑,我正在工厂寻找一个可行的解决方案。谢谢。我读到的每一件事都表明,为了避免控制器出现所有业务逻辑,我正在工厂寻找一个可行的解决方案。谢谢。我读到的每一件事都表明,为了避免控制器出现所有业务逻辑,我正在工厂寻找一个可行的解决方案。谢谢。我读到的每一篇文章都说,为了避免控制器出现所有业务逻辑,我正在工厂里寻找一个可行的解决方案。谢谢,不仅是因为答案,还因为代码中的精彩评论。这将是一个很好的例子,为其他人在工作时的承诺以及。感谢,不仅为答案,而且为伟大的评论在代码中。这将是一个很好的例子,为其他人在工作时的承诺以及。感谢,不仅为答案,而且为伟大的评论在代码中。这将是一个很好的例子,为其他人在工作时的承诺以及。感谢,不仅为答案,而且为伟大的评论在代码中。这将是一个很好的例子,其他人在工作时的承诺以及。