2015-01-28 4 views
0

저는 javaScript와 html을 처음 사용합니다. javascript의 html head 태그에 스크립트 값을 동적으로 추가하는 방법

$http.get(URL).success(function (response) { 
      $scope.trailers = response; 
     var js = document.createElement('script'); 
     js.setAttribute('type', 'text/javascript'); 
     js.value = "api_gallery = ["+trailers[0].youtubeId +", "+trailers[1].youtubeId +"];api_titles = ['video1', 'video2'];api_descriptions = ['description1', 'description2'];"; 

     document.getElementsByTagName('head')[0].appendChild(js); 
}); 

가 실제로 위의 코드입니다 내가 동적 스크립트 값을 만들려고하고 자바 스크립트에서 헤드 태그를 HTML로 추가하고 난 다음과 같은도 AngularJS와를 사용하고 있어요 ..

내 script.js

youtube videos popup.for 나는 위의 스크립트에 YouTube URL을 할당해야합니다.

내 세부 JSON

사전에

[{"youtubeId": "https://www.youtube.com/watch?v=_eZh4R_6WtA"},{"youtubeId": "https://www.youtube.com/watch?v=ynDAZ8-xcSE"}] 

덕분에 ... 내가 js.value이 확인 다른 모든 것을 작동하지 않을 생각

+0

는'이 값 '스크립트 요소는'이없는 innerHTML' – adeneo

+0

http://weblogs.asp.net/dwahlin/creating-custom-angularjs-directives-part-3-isolate-scope- and-function-parameters –

+0

답장을 보내 주셔서 감사합니다 ..... 나는 해결책을 얻었습니다 ..... 고마워요 – Srikanth

답변

0

입니다. 아래에 자바 스크립트 코드 을 추가하는 appendChild 메소드를 사용하면 javascript 코드를 머리에 추가 할 수 있습니다.

var txt = "alert('hello');"; 

var scriptTag = document.createElement("script"); 
scriptTag.setAttribute("type", "text/javascript"); 

// append it in a text node 
scriptTag.appendChild(document.createTextNode(txt)); 
document.getElementsByTagName("head")[0].appendChild(scriptTag); 
+0

고마워 ...... ...... 알 겠어. – Srikanth

관련 문제