在尝试遵循Angular Style Guide(https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#data-services)时,我遇到了正确使数据服务正常工作的问题
我确信这对于经验丰富的人来说是明显的,但是我无法将数据集正确地分配给
之外的vm.items。数据服务
import java.util.Random;
public class Craps{ public static void main(String [] args){
Boolean winLose = false;
int dice1 = (int)(Math.random()*6) + 1;
int dice2 = (int)(Math.random()*6) + 1;
int sum = dice1 + dice2;
if ((sum == 7) || (sum == 11)){
System.out.println("You Win!");
return;}
else if ((sum == 2) || (sum == 3) || (sum == 12)){
System.out.println("You Lose!");
return;}
while(!winLose){
System.out.printf("Your Point: %d\n", sum);
dice1 = (int)(Math.random()*6) + 1;
dice2 = (int)(Math.random()*6) + 1;
int point = dice1 + dice2;
System.out.printf("You rolled: %d %s %d %s %d\n",dice1,"+",dice2,"=",point);
if (point == sum){
winLose = true;
System.out.println("You Win!");
}
if (point == 7){
winLose = true;
System.out.println("You Lose!");
}
}
.
.
.
.(complete the codes..)
控制器
(function() {
'use strict';
angular
.module('portfolioApp')
.factory('portfolioService', portfolioService);
portfolioService.$inject = ['$http', 'logger'];
function portfolioService($http, logger) {
return {
getPortfolioData: getPortfolioData,
};
function getPortfolioData() {
return $http.get('./assets/portfolio/portfolioItems.json')
.then(getPortfolioDataComplete)
.catch(getPortfolioDataFail);
function getPortfolioDataComplete(response) {
return response.data;
}
function getPortfolioDataFail(error) {
logger.error('XHR Failed for getPortfolioData.' + error.data);
}
}
}
}());
答案 0 :(得分:1)
您的getData
函数是一个承诺,因此它是异步运行的。在承诺结束之前调用console.log
,因此vm.items
仍为空。
尝试将日志放入then
回调。