2016-05-18 18 views
1

jQueryとAngularを動的に読み込む匿名のjavascript関数を記述しました。しかし、私の角度コードは何らかの理由で実行されていません。私のmain機能にヒットするまで、コードは検索されます。私はhttp要求をして、私の角度コントローラーで結果を印刷しようとしますが、何らかの理由で印刷していません。誰かが助けることができますか?ここに私のバイオリンとコードは以下のとおりです。事前にAngularjsコードが匿名関数で実行されていない

https://jsfiddle.net/5f44yb7k/1/

ありがとう!

(function() { 

    var jQuery; 
    var jquery_tag = document.createElement('script'); 
    var angular_tag = document.createElement('script'); 

    function getJqueryTag() { 
    console.log('getting jquery tag'); 
    jquery_tag.setAttribute("type","text/javascript"); 
    jquery_tag.setAttribute("src", "https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"); 
    jquery_tag.onload = scriptLoadHandler; 
    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(jquery_tag); 
    if(jquery_tag.onload) { //ensure angular loads after jQuery 
      getAngularTag(); 
    } 

    } 

    function getAngularTag() { 
    console.log('now getting angular tag...'); 
    angular_tag.setAttribute("type","text/javascript"); 
    angular_tag.setAttribute("src", "https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"); 
    angular_tag.onload = scriptLoadHandler; 
    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(angular_tag); 
    } 

    getJqueryTag(); 


    function scriptLoadHandler() { 
    if(window.jQuery) { 
     jQuery = window.jQuery 
     main(); 
    } 
    } 

    function main() { 
    jQuery(document).ready(function($) { 
     console.log('now in main function'); //this prints fine 
     var test = angular.module('test', []); 

     function main_controller($scope, $http) { 
     $http.get('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22fairfax%2C%20va%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys') 
      .success(function(data) { 
      console.log('---data returned---'); //not printing 
      console.log(data); //not printing 
      }) 
      .error(function(data) { 
      console.log('error: ' + data); 
      }); 
     } 
    }); 
    } 

})(); 

答えて

2

角度が範囲外になっています。このようにしてロードする場合は、ブートストラップ関数を呼び出して角度を取得して実行する必要があります。

この方法でロードしたことはありませんが、https://docs.angularjs.org/guide/bootstrapを参照してください。具体的には、マニュアルの初期化に関するセクションをご覧ください。

+0

これを実行しましたか?もしそうなら、変更したものを投稿できますか? –

+0

まだ、もう一度やり直すよ –

関連する問題