i new angular , have question angular ajax call.
i struggle service call fires after directive. , can´t work properly. code works hardcoded json-data in controller idont know how setup service/factory remote service.
here working sample hardcoded json. code explained: have list of item in view , want show toolbox, on hover each item. use jquery.qtip.js so. use directive show toolbox , content controller json. (in function getcurrentbooklist whant mark in toolboxlist name same.)
var listcontrollers = angular.module('listcontrollers', []); listcontrollers.controller("startctrl", function ($scope) { $scope.booklist = [ { name: 'john', age: 25, gender: 'boy' }, { name: 'jessie', age: 30, gender: 'girl' }, { name: 'johanna', age: 28, gender: 'girl' }, { name: 'joy', age: 15, gender: 'girl' }, { name: 'mary', age: 28, gender: 'girl' }, { name: 'peter', age: 95, gender: 'boy' }, { name: 'sebastian', age: 50, gender: 'boy' }, { name: 'erika', age: 27, gender: 'girl' }, { name: 'patrick', age: 40, gender: 'boy' }, { name: 'samantha', age: 60, gender: 'girl' } ]; $scope.gettooltips = function (bookid) { return getcurrentbooklist(bookid, $scope.booklist); } }); listcontrollers.directive('qtip', function () { return { restrict: 'a', link: function (scope, element, attrs) { element.qtip({ content: { text: scope.gettooltips(attrs.qtip) }, position: { my: 'bottom center', at: 'top center' }, hide: { fixed: true, delay: 300 } }); } }; }); var getcurrentbooklist = function (name, arr) { var rettext = "<ul>"; (i = 0; < arr.length; i++) { if (name== arr[i].name) { rettext += "<li>" + arr[i].name + " - mark </li>"; } else { rettext += "<li>" + arr[i].name +"</li>"; } } rettext += "</ul>"; return rettext; };
thanks
when want use service/factory remote data, call $http returns promise object. please check $http.get docs here. call further actions after data fetched done when promise resolves/fails.
Comments
Post a Comment