javascript - Delay directive/div until scope value available -
i have custom directive soundcloud requires soundcloud url. soundcloud url fetched database through $http service, however, div soundcloud custom directive loaded , requires value of soundcloud url before defined.
the plangular directive code got here: https://github.com/jxnblk/plangular/blob/master/src/plangular.js *i did not develop this
this html code:
<div plangular="{{soundcloud}}"> <button ng-click="playpause()">play/pause</button> <progress ng-value="currenttime / duration || 0"> {{ currenttime / duration || 0 }} </progress> </div>
and angular code:
displaysong.controller('song', ['$scope', '$http', 'fetchsong', function($scope, $http, fetchsong) { $scope.songid $scope.songname; //controller properties $scope.songpromise; //the song promise fetching $scope.init = function(songid, userid) { $scope.songid = songid; $scope.userid = userid; $scope.songpromise = $http({ method: "post", url: fetchsong, data: { song_id: $scope.songid }, headers: { 'content-type': 'application/x-www-form-urlencoded' } }).then(function(successresponse) { console.log('successfully fetched song'); console.log(successresponse); var song = successresponse.data; $scope.songid = song.song_id; $scope.songname = song.song_name; $scope.songtype = song.song_type; $scope.songembed = song.song_embed; $scope.soundcloud = song.song_embed; }, function(errorresponse) { console.log('error fetching'); $scope.songid = null; }); }; }]);
i know it's problem asynchronous nature because when add line in beginning of song controller:
$scope.soundcloud = "https://soundcloud.com/jshigley/shine";
it works fine. i've noticed when spam play/pause button come directive, multiple console errors of "http 404 not found", leads me believe it's trying find track of undefined url
since it's div directive , not function call can't use promises such chaining $scope.songpromise. i've thought of putting controller , having controller $timeout 5 seconds, don't think delays execution of dom.
the soundcloud url end getting loaded, remains undefined in eyes of plangular directive (i've encountered lots of these problems bad timing of loading scope , directives). angular wizards willing teach me how tame asynchronous nature of angularjs?
you can use $watch in custom directive watch when url attributes changed. in
link: function(scope, el, attr) {
change
if (src) { resolve({ url: src, client_id: client_id }, function(err, res) { if (err) { console.error(err); } scope.$apply(function() { scope.track = createsrc(res); if (array.isarray(res)) { scope.tracks = res.map(function(track) { return createsrc(track); }); } else if (res.tracks) { scope.playlist = res; scope.tracks = res.tracks.map(function(track) { return createsrc(track); }); } }); }); }
to
scope.$watch('attr.plangular', function(newval) { resolve({ url: attr.plangular, client_id: client_id }, function(err, res) { if (err) { console.error(err); } scope.$apply(function() { scope.track = createsrc(res); if (array.isarray(res)) { scope.tracks = res.map(function(track) { return createsrc(track); }); } else if (res.tracks) { scope.playlist = res; scope.tracks = res.tracks.map(function(track) { return createsrc(track); }); } }); }); }, true);
if dont want change directive might want use ng-if load plangular div when url.
<div plangular="{{soundcloud}}" ng-if="haveurl">
and in angular code :
}).then(function(successresponse) { console.log('successfully fetched song'); console.log(successresponse); $scope.haveurl = true;
Comments
Post a Comment