2016-12-08 5 views
0

単純な例を明確にしてください: この例では、angularApp.jsで計算されたmachineLengthを使用するにはどうすればよいですか? 今のところ、angleApp.jsで計算されたものではなく、空のオブジェクトを出力します。他のjsファイルで計算された変数にアクセスする方法は?

<html> 
    <head> 
    <script src="...">...</script> 
    <script> 
     let machineLength={}; 
    </script> 
    <script src="angularApp.js"></script> 
    </head> 
    <body> 
    ... 


    <script> 
     console.log(machineLength); 
     // here is a lot of code use machineLength to render scene of models with three.js 
     //the machineLength is the render region width, height. length 
    </script> 
    </body> 
</html> 

angularApp.js:

let sliceApp = angular.module('sliceConfig', []); 
    sliceApp. 
    .controller('sliceConfigController', ['$scope', '$http', function($scope, $http) 
    { 
     $http.get("http://xxxxx/getSliceConfig") 
      .success(function(response) 
      { 
      $scope.config = []; 
      $scope.config.machine_width = machineLength.x = parseFloat(response.config.machine_width); 
      ... 
      } 
    }]); 

どれが助けが理解されるであろう!応答が到着する前に、問題がangularApp.jsあるProviders value recipe

var myApp = angular.module('myApp', []); 
myApp.value('m_width', '100'); //some init value 

myApp.controller('DemoController', ['m_width', function DemoController(m_width) { 

$http.get("http://xxxxx/getSliceConfig") 
     .success(function(response) 
     { 
     $scope.config = []; 
     var x = //something from response; 
     $scope.$emit('eventName', { width: x }); 
     ... 
     } 

}]); 

myApp.controller('otherController', ['m_width', function otherController(m_width) { 

    $scope.$on('eventName', function (event, args) { 
    $scope.message = args.width; 
    console.log($scope.message); 
    }); 

    $scope.width = m_width; 

}]); 
+2

angular.valueは、あなたがより多くの情報が得られますA.J @ –

+0

を助けるかもしれない、angular.valueをどのように使用する:

代わりに、私は、値プロバイダーを使用することをお勧めしますか?ありがとう –

+0

私は1時間後の例を投稿するつもりで、ノートパソコンにアクセスできない。ありがとうございます(現時点でモバイル通信を使用しています);) –

答えて

1

を使用する必要が

+0

ええ、これはconsole.log(machineLength)で機能します。しかし実際には、ここではmachineLengthを使用するシーンをレンダリングするコードがたくさんあります。 machineLengthレンダリングをブロックするにはどうしたらいいですか? –

+1

そのコードを 'angularFinishedJob()'に追加するか、 'angularFinishedJob()'からレンダリングしている関数を呼び出してください。 –

1

は、非同期AJAX呼び出しを使用して、あなたのメインページを印刷machineLengthでスクリプトを実行します。

解決策が到着したときに、angularAppスクリプトがメインページのスクリプトに通知する方法があります。

... 
.success(function(response) { 
     $scope.config = []; 
     $scope.config.machine_width = machineLength.x = parseFloat(response.config.machine_width); 
     angularFinishedJob(); // call the script in the main page when job is done 
... 

メインページ:

<script> 
    console.log(machineLength); // empty object, because angular job didn't finish 

    function angularFinishedJob(){ // called from angularApp.js 
     console.log(machineLength); // calculated object 
     // add code which uses machineLength 
    } 
</script> 

注:angularApp.jsで、私はあなたがangularApp.jsを変更へのアクセス権を持っている場合、これは実装する最も簡単な方法だと思います。そうでない場合は、他のソリューションは

+0

次のレンダリングでinit値を使用する場合、machineLengthはレンダー領域の幅、高さです。長さシーンはinitの値で初期シーンを描画し、$ http.getが返ってきたらシーンを再レンダリングします。 –

+0

これは、コントローラ間の変数にアクセスしたいときに、あなたの質問に答えることでした。しかし、あなたのユースケースは異なります。あなたの要件のために、私はむしろ$ emitと$をウォッチ式で使用したいと思います。 – sisyphus

+0

あなたの時間と情報ありがとうございます。あなたはそれについてもっと詳しく見せますか? –

1

を注入$ウィンドウを存在し、それを使用:

let sliceApp = angular.module('sliceConfig', []); 
    sliceApp. 
    .controller('sliceConfigController', function($window, $scope, $http) 
    { 
     $http.get("http://xxxxx/getSliceConfig") 
      .success(function(response) 
      { 
      $scope.config = []; 
      //$scope.config.machine_width = machineLength.x = parseFloat(response.config.machine_width); 
      $scope.config.machine_width = $window.machineLength.x = parseFloat(response.config.machine_width); 
      ... 
      } 
    }); 

私は値を格納するためのグローバルオブジェクトまたは$rootScopeのいずれかを使用することはお勧めしませんが、それは動作します。

let sliceApp = angular.module('sliceConfig', []); 

    sliceApp.value("machineLength", {}); 
    sliceApp. 
    .controller('sliceConfigController', function(machineLength, $scope, $http) 
    { 
     $http.get("http://xxxxx/getSliceConfig") 
      .success(function(response) 
      { 
      $scope.config = []; 
      $scope.config.machine_width = machineLength.x = parseFloat(response.config.machine_width); 
      ... 
      } 
    }); 
関連する問題