javascript - Angular asynchronous assinging href to a link -


i need update href of link asynchronously. let's i've got html:

<a ng-href="{{url}}" ng-click="resolveurl()" target="_blank">click me</a> 

and angular controller:

app.controller('mainctrl', function($scope, $q) {   $scope.url = '#';    $scope.resolveurl = function() {     async().then(function(resolvedurl){       $scope.url = resolvedurl;     });   }    function async() {     var deffered = $q.defer();      settimeout(function() {       deffered.resolve('http://www.google.com');     }, 1000)      return deffered.promise;   } }); 

and need link go google.com first time. how synchronize async call or how assign url async function resolves?

code in plunker

edited:

as @konstantin krass pointed out use window.open(). cannot use asynchronously because browsers popup block new window. i've tried open window on-click (which not blocked) , after promise resolved update url of opened window google.com. edited plunker here. unfortunately not work on ipad. on ipad page url not updated because on ipad tabs can't communicate each other. ideas?

if want calculate url , redirect url after click, use native location.href after receiving url.

app.controller('mainctrl', function($scope, $q) {         $scope.resolveurl = function() {     async().then(function(resolvedurl){       location.href= resolvedurl; //this open url in current page.       // if want offer download or move        // new browser tab can go this:        // window.open(resolvedurl);       });   }    function async() {     var deffered = $q.defer();      settimeout(function() {       deffered.resolve('http://www.google.com');     }, 1000)      return deffered.promise;   } }); 

and remove {{url}} html.

<a ng-click="resolveurl()">click me</a> 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -