2017-06-20 10 views
1

他のオブジェクトの内部にあるオブジェクトにアクセスしたいのですが、それを操作するたびに「未定義」と表示されます。 例他のオブジェクトのオブジェクトへのアクセス

$scope.instrumento = new Instrumento(); 

$scope.empresaInstrumento = EmpresaInstrumento.get({id:$routeParams.id}, function (instrumento) { 
    $scope.instrumento = instrumento.instrumento; 
    console.log($scope.instrumento); 

}); 
console.log($scope.instrumento); 
私は彼の参照を失うことなく「empresaInstrumento」のうち「instrumento」このオブジェクトを取得できますか

方法は、APIから情報を取得する取得)

+0

HTMLで正しく示しては、お使いのコンソールinstrumentoを行なったし、それがinstrumento持っているかどうかを確認しますか? –

+0

はい、あります。 "empresaInstrumento"にはオブジェクト "empresa"と "instrumento"が表示されます。 私はコンソールempresaInstrumento.instrumentoが '未定義'を表示し続けているが、その機能の中に正確に計測器が表示されます –

+0

どの角度の角度ですか?あなたのサービスEmpresaInstrumento.getは$ httpまたはカスタム約束を使用していますか? – gyc

答えて

0

機能empresaInstrumentoinstrumentoという名前のプロパティを持っていないためです。

このように、より良いアプローチは次のようなことです。あなたのコードで

$scope.instrumento = new Instrumento(); 

$scope.empresaInstrumento = { 
// create an instrumento property 
    instrumento: function() { 
     var toReturn = {}; 
     EmpresaInstrumento.get({id:$routeParams.id}, function (instrumento) { 
      toReturn = instrumento.instrumento; 
     }); 

     return toReturn; 
    } 
} 
console.log($scope.empresaInstrumento.instrumento()); 
1

あなたは$scope.empresaInstrumento内で実行するfunctionを持っていなかった、あなたは、サンプルを参照してください、functionとしてそれを作る、その後ng-initとして、コントローラやビューからそれを呼び出す必要があり

app.controller("ctrl", ["$scope", "EmpresaInstrumento", "$routeParams", function ($scope, empresaInstrumento, routeParams) { 
    $scope.instrumento = {}; 

    $scope.empresaInstrumento = function() { 
     empresaInstrumento.get({ id: routeParams.id }, function (instrumento) { 
      $scope.instrumento = instrumento.instrumento; 
      console.log($scope.instrumento); 
     }); 
    } 

    $scope.empresaInstrumento(); //or in ng-init="empresaInstrumento()" 

    console.log($scope.instrumento); 
}]); 
1

私はあなたのhtmlページで更新されているスコープの値を表示していないと推測していると、値が正しく設定されているかどうかをテストするconsole.logsを追加しました。

あなたは2つの問題を抱えて:APIの呼び出しは、任意のデータを返す前に

1 - 第二にconsole.logが実行されます。だからあなたは未定義になる。

2つのEmpresaInstrumento.getがどのようにコード化されているかによって、スコープの値を$ scope $に更新する必要がある場合があります。適用();

$scope.instrumento = new Instrumento(); 

$scope.empresaInstrumento = EmpresaInstrumento.get({id:$routeParams.id}, function (instrumento) { 
    $scope.instrumento = instrumento.instrumento; 
    console.log($scope.instrumento); 
    $scope.$apply(); // update the scope and your view 
}); 
console.log($scope.instrumento); // still undefined here 

最初にconsole.logは未定義示ししかし、あなたはあなたのサービスに問題があると我々はそれをデバッグするコード化されています方法を確認する必要がある場合

+0

ありがとうございます!私がやっていたやり方はもっと楽しいようです。 コードをリファクタリングするときに、後で試してみます –

0

マヘルが言ったように、私は。応答をキャッチするためにローカル変数を使用し、コールバック内の名前付き関数を呼び出して$に送信する情報を保存することができましたスコープ変数は、ローカル変数を使用して関数で作成されます。

このような何か:私は、その情報を要求するとき

$scope.empresaInstrumento = function() { 
 
     empresaInstrumento.get({ id: routeParams.id }, function (instrumento) { 
 
      $scope.instrumento = instrumento.instrumento; 
 
      $scope.loadInstrumento($scope.instrumento); 
 
     }); 
 
    } 
 
    
 
    
 
    $scope.loadInstrument = function loadInstrument(element) { 
 
     $scope.instrumento = element; 
 
    }

だから、それは

関連する問題