2016-12-06 6 views
0

AngularJSを使用してNetBeans を使用してJavaでWebアプリケーションを開発しています。

が、コントローラで

、私は届かない、私は非常によく働いて、私は私が必要とするオブジェクトとJSON配列を取得していますローカルホストで私のWebサービスにアクセスしています情報Webブラウザの

ログイン:

Result: [object Object] OR {} script.js:140:5

Success/Error: undefined

コード:

minhaAplicacao.controller('inicioPacienteCTRL', function ($scope, $http) { 
    $scope.medicoSelecionado; 

    var aux; 
    var $scope.result = window.console.log($http.get("http://localhost:8080/Clinica5/webresources/medicos")).then(function (success) { 
     aux = success; 
    }, function (error) { 
     aux = error; 
    }); 

    window.console.log("Result: "+$scope.result+ " OR "+JSON.stringify($scope.result)); 
    window.console.log("Success/Error: "+aux); 
}); 

そして、私はビューでこのコードを入れた場合、私はエラーを得た:

<div ng-bind="$scope.result"></div> 

Error: $scope.result is not defined

私は

どうもありがとう< 3ビッグ$ routeProviderを設定し、絶対的に正しいことをしています抱擁!

+0

可能な重複redirectTo行を削除して、[応答がangularjsで、$のHTTPリクエストから来るまで待機する?](http://stackoverflow.com/questions/18421830/how-to-wait-to-the-response-from-http-request-in-angularjs) – Jigar7521

+0

これは、応答が来るまで$ scope.resultが初期化されるために起こります。応答まで待たずに来る、それが理由だ。 – Jigar7521

答えて

0

あなたはまた、あなたが$スコープ変数を宣言するとき、VARの使用を避けることができ、この

$scope.result = success 

のような非同期要求の結果としてスコープ変数の結果をコントローラにあなたの応答を割り当てる必要が

minhaAplicacao.controller('inicioPacienteCTRL', function ($scope, $http) { 
$scope.medicoSelecionado; 

$scope.aux = {}; 
$scope.result {}; 
$http.get("http://localhost:8080/Clinica5/webresources/medicos").then(function (success) { 
    $scope.result = success; 
}, function (error) { 
    $scope.aux = error; 
}); 

window.console.log("Result: ",$scope.result, " OR ",JSON.stringify($scope.result)); 
window.console.log("Success/Error:",$scope.aux); 

}) ;ビューでも

<div ng-bind="result"></div> 

0

は、2番目のエラーのため<div ng-model="result"></div>を試してみてください$スコープの必要はありません。

てみませんかする必要はありません:

$scope.medicoSelecionado; 
$scope.aux; 
$scope.result; 

あなたがものを必要なときだけ新しいモデルを使用します。宣言は必要ありません。

あなたの.then()の$scope.result = successを行うことは、Vinod Louisの示唆したように、うまくいくはずです。私はそれを行うだろう

方法:

minhaAplicacao.controller('inicioPacienteCTRL', function ($scope, $http) { 

    $http.get("http://localhost:8080/Clinica5/webresources/medicos") 

    .then(function (success) { 
       $scope.result = success; 
      }, function (error) { 
       $scope.result = error; 
      }); 

    window.console.log("Result: "+$scope.result+ " OR "+JSON.stringify($scope.result)); 

}); 

auxが道で何をしますか?

0

あなたは何も定義されていない場合、それは
未定義が表示されます、それはあなたが次のように試すことができます[object, object]

minhaAplicacao.controller('inicioPacienteCTRL', function ($scope, $http) { 
$scope.medicoSelecionado; 

var aux = {}; 
var $scope.result = window.console.log($http.get("http://localhost:8080/Clinica5/webresources/medicos")).then(function (success) { 
    aux = success; 
}, function (error) { 
    aux = error; 
}); 

window.console.log("Result: "+$scope.result+ " OR "+JSON.stringify($scope.result)); 
window.console.log("Success/Error: "+aux); 
}); 
1

を示しているように、あなたは成功でオブジェクトを取得しているので、var aux = {}を定義する必要があります。

minhaAplicacao.controller('inicioPacienteCTRL', function ($scope, $http) { 

    $scope.functionName = function(){ 
     //define 
     $scope.medicoSelecionado = {}; 
     $http.get("http://localhost:8080/Clinica5/webresources/medicos").then(function (success) { 
      console.log(success); 
      //success data passed 
      $scope.medicoSelecionado = success; 

     }, function (error) { 
      console.log(error); 
      //error message 
      $scope.error = error; 
     }); 

    } 
}); 

とエラー

<div class="error">{{error}}</div> 
0

GOTを表示するには、このHTMLを使用!

何らかの理由で、「ログイン」ルートと競合していました。なぜ私は知りません。

ソリューションの

when('/inicioMedico', { 
    templateUrl: 'inicioMedico.html', 
    controller: "inicioMedicoCTRL" 
}). 
otherwise ({ 
    // redirectTo: 'login' ERROR HERE 
}); 
関連する問題