Note that there are some explanatory texts on larger screens.

plurals
  1. POAngularjs $http wait for response
    primarykey
    data
    text
    <p>I am a newbie to javascript/angularjs. I want to show a bootstrap popover when mouseover is done on some elements. </p> <p>I created a directive for that</p> <pre><code>(function(angular, app) { app.directive('popOver',["$window","$http",function($window,$http){ return function(scope,elem,attrs){ elem.on('mouseover',function(){ console.log('mouseover'); var data = scope.$apply(attrs.popOver); console.log('in directive again'); console.log(data); }); }; }]); })(angular, app); </code></pre> <p>the value of the directive is a function </p> <p><code>&lt;span class="asdf" pop-over="getVCard(id)"&gt;name&lt;/span&gt;</code></p> <p>the function vcard(id) is defined in my angularjs controller. It checks if the data is already there in localstorage and if present, returns the data. otherwise it does a $http get request and stores the data in localstorage. </p> <pre><code>$scope.getVCard = function(id){ var vcardKey = vcardKeyPrefix+id; var vCardFromLS = localStorageService.get(vCardKey); if(vCardFromLS){ return localStorageService.get(vCardKey); } $http.get(app.common.vcardUrl(id)). success(function(data){ localStorageService.set(vCardKey,data); }). error(function(error){ F1.common.error(data,function(){}); }); return localStorageService.get(vCardKey); }; </code></pre> <p>Since $http returns promise, i am getting a value of undefined in my directive. How can i make sure that the function <code>getVCard</code> returns synchronously?</p> <p>I read a number of results after googling. But the suggested idea is to use callbacks. But i am not sure how callbacks will make this synchronous. Any help is appreciated.</p> <p><strong>UPDATE 1</strong> (in response to Foo L's comment) i was planning to use the same directive in multiple places. popovers for vcards and product information and so on. The only thing that will differ is the argument to the pop-over directive. That way the directive will bother about just displaying the popup. Where to get the data will be in a separate function that is passed to the directive</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload