angularjs - How do I pass the variable in a function into my controller? -
i'm trying pass videourl variable in showresponse function controller. i've been trying figure out solution without success. can guide me in right direction?
var myapp = angular.module('myapp', []); myapp.controller('mainctrl', ['$scope', function($scope){ $scope.videourl = videourl; }]) // helper function display javascript value on html page. function showresponse(response) { var videourl = []; (prop in response.items) { videourl[prop] = "https://www.youtube.com/embed/" + response.items[prop].snippet.resourceid.videoid; } } // called automatically when javascript client library loaded. function onclientload() { gapi.client.load('youtube', 'v3', onyoutubeapiload); } // called automatically when youtube api interface loaded function onyoutubeapiload() { gapi.client.setapikey('#######'); search(); } function search() { // use javascript client library create search.list() api call. var request = gapi.client.youtube.playlistitems.list({ part: 'snippet', playlistid: '########' }); // send request api server, // , invoke onsearchrepsonse() response. request.execute(onsearchresponse); } // called automatically response of youtube api request. function onsearchresponse(response) { showresponse(response); }
it better/easier if stuff into angular, it's happening within services. that's how data sharing supposed happen in angular. maybe that's challenging due nature of onclientload
. dirty way is:
get controller's scope directly , set on scope. assuming you've got defined like:
<div ng-controller="mainctrl"></div>
you can controller's scope using jquery:
function showresponse(response) { var videourl = []; (prop in response.items) { videourl[prop] = "https://www.youtube.com/embed/" + response.items[prop].snippet.resourceid.videoid; } var scope = $('[ng-controller="mainctrl"]').scope(); scope.videourl = videourl; }
note cause angular purists weep , gnash teeth.
Comments
Post a Comment