该值在元素为对象的数组中消失

时间:2017-03-28 05:36:57

标签: angularjs

以下是代码:

$scope.get_candidate();
console.log($scope.voteData);

$scope.get_candidate = function () {
            var postData = {
                department: $scope.voteData.department,
                group: $scope.voteData.group
            };
            $http.post('/admin/r_candidate', postData)
                .success(function (response) {
                    $scope.voteData.candidates = response.data.candidateInfos;
                });
        };

控制台打印:

Object {candidates: Array[0], vote_begin: true, department: "机械与运载工程学部", vote_type: "预选", ballot_type: "记分"}

我们可以看到候选者是一个空数组,当我使用$ scope.voteData发出http post请求时,候选者是空的。但是当我看到细节时:

Object
ballot_type:"记分"
candidates:Array[4]
department:"机械与运载工程学部"
vote_begin:true
vote_type:"预选"
__proto__:Object

我们可以看到候选人有四个元素,这是我所期望的因为$ http.post('/ admin / r_candidate',postData)返回:

 {status: 0, data: {candidateInfos: [,…], total: 4}}
data:{candidateInfos: [,…], total: 4}

我不知道为什么候选人会成为一个空数组。

2 个答案:

答案 0 :(得分:3)

使用异步javascript时,这是一个反复出现的逻辑问题。

运行时

<Page class="page" xmlns="http://www.nativescript.org/tns.xsd" navigatedTo="onNavigatingTo">
<ScrollView>
  <GridLayout rows="auto,*, auto, *,auto, *, auto" height="100%" columns="*, *" width="100%">
    <Label class="label-bold" class='primeiros' row="0" col="0" text="O que foi positivo?" textWrap="true"></Label>
    <Label class="label-bold" class='primeiros' row="0" col="1" text="O que foi positivo?" textWrap="true"></Label>
    <TextView row="1" col="0" backgroundColor='gold' class='primeiros' returnPress="adicionar" textWrap="true" class="input"
      text="{{ lista.positivo }}"></TextView>
    <TextView row="1" col="1" backgroundColor='gold' class='primeiros' returnPress="adicionar" textWrap="true" class="input"
      text="{{ lista.negativo }}"></TextView>
    <Label class="label-bold" row="2" colSpan="2" text="Minhas ações a respeito:" textWrap="true"></Label>
    <TextView returnPress="adicionar" row="3" colSpan="2" text="{{ lista.acoes }}"></TextView>
    <Label class="label-bold" row="4" colSpan="2" text="Minha oração sobre esse dia:" textWrap="true"></Label>
    <TextView returnPress="adicionar" row="5" colSpan="2" text="{{ lista.oracao }}"></TextView>
    <Button text="Concluir Este Dia" row="6" colSpan="2" class="btn btn-primary" tap="concluirDia" />
  </GridLayout>
  <StackLayout [height]="keyboardHeight" width="100%"></StackLayout>
</ScrollView>

它会比{http {1}}

的结果更快地运行$scope.get_candidate(); //async data inside console.log($scope.voteData);

您可以/应该处理console.log()函数

中的结果
request

这就是说,根据你的angularjs版本,你应该使用the .then syntax

答案 1 :(得分:-1)

这里的问题是你使用异步方法你的控制台在返回结果之前输出输出,你可以使用$ q来处理异步函数

public class JdbcDBManager {
private Connection connection = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;

public JdbcDBManager(String url, String user, String pass) throws ClassNotFoundException, SQLException {
    Class.forName("org.gjt.mm.mysql.Driver");
    this.connection = DriverManager.getConnection(url, user, pass);
}

public void close() {
    try {if (this.preparedStatement != null)this.preparedStatement.close();} catch (Exception e) {e.printStackTrace();}
    try {if (this.resultSet != null)this.resultSet.close();} catch (Exception e) {e.printStackTrace();}
    try {if (this.connection != null)this.connection.close();} catch (Exception e) {e.printStackTrace();}
}
public void customerInsert(Customer customer) {
    try {
        String query = "INSERT INTO customer(email,product) VALUES(?,?,?,?,?)";
        this.preparedStatement = this.connection.prepareStatement(query);
        this.preparedStatement.setString(1, customer.getEmail());
        this.preparedStatement.setString(3, customer.getProduct());
    } catch (Exception e) { e.printStackTrace();}
}}