Collection Repeat w/ $http and $resource Factory Example
How do I make an collection repeat w/ $http and $resource factory example?
Using https://developer.nutritionix.com/ for nutrition information; showing how to access REST API with either $http or $resource with IonicFramework collection-repeat directive.. What is a collection repeat w/ $http and $resource factory example? How do you make a collection repeat w/ $http and $resource factory example? This script and codes were developed by Aaron K Saunders on 08 September 2022, Thursday.
Collection Repeat w/ $http and $resource Factory Example - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Collection Repeat w/ $http and $resource Factory Example</title>
</head>
<body> <html ng-app="ionicApp"> <head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> <title>Collection Repeat w/ $http and $resource Factory Example</title> <link href="//code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet"> <script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script> <script src="https://code.angularjs.org/1.3.9/angular-resource.min.js"></script> </head> <body> <!-- Create the header bar --> <ion-nav-bar class="bar-positive"> <ion-nav-back-button class="button-icon ion-arrow-left-c"> </ion-nav-back-button> </ion-nav-bar> <!-- this is where the content from application is displayed --> <ion-nav-view></ion-nav-view> <script id="home.html" type="text/ng-template"> <!-- add a search input field on top of page --> <div class="bar bar-subheader item-input-inset"> <label class="item-input-wrapper"> <input type="search" placeholder="Search text" ng-model="data.searchKey"/> </label> <!-- when user clicks, do a search --> <button class="button " ng-click ="doSearch()">Search </button> </div> <!-- The title of the ion-view will be shown on the navbar --> <ion-view view-title="Nutrition Search"> <ion-content class="has-subheader"> <!-- The content of the page --> <div class="list"> <!-- use collection-repeat to display content --> <div class="item my-item" collection-repeat="item in items" collection-item-width="'100%'" collection-item-height="getItemHeight(item, $index)" ng-style="{height: getItemHeight(item, $index)}"> {{item.fields.brand_name}} - {{item.fields.item_name}} - {{item.fields.nf_calories}} </div> </div> </ion-content> </ion-view> </script> </body>
</html> <script src="js/index.js"></script>
</body>
</html>
Collection Repeat w/ $http and $resource Factory Example - Script Codes JS Codes
angular.module('ionicApp', ['ionic','ngResource']) .value('nutritionixConst', { 'appId' :'8abbcd8e', 'appKey' : '36e8d264537037ee7e832a41902ffe57'
})
/**
* sample using collection repeat and data is provided using $htp and $resource
*
* additional documentation on collection-repeat
* - https://ionicframework.com/docs/api/directive/collectionRepeat/
*/ .controller('HomeCtrl', function($scope, DataService,DataServiceHTTP, Weather) { $scope.data = {searchKey:''}; $scope.getItemHeight = function(item, index) { //Make evenly indexed items be 10px taller, for the sake of example return (index % 2) === 0 ? 50 : 60; }; /** * */ $scope.doSearch = function() { console.debug("Searching for: " + $scope.data.searchKey); if ( true ) { // use the $resource based service var promise = DataService.getAll( { 'term' : $scope.data.searchKey, 'results':'0:50', // <-- variable substitution //'fields':'item_name' <-- you can specify field params }).$promise; promise.then(function(_response) { console.debug(" The data " + JSON.stringify(_response)); $scope.items = _response.hits; }); } else { // use the $http based service var promise = DataServiceHTTP.getAll($scope.data.searchKey); promise.then(function(_response) { console.debug(" The data " + JSON.stringify(_response.data)); $scope.items = _response.data.hits; }); } };
})
/**
*
*/ .factory('DataService', function( $resource, nutritionixConst){ var aSearchObject = $resource('https://api.nutritionix.com/v1_1/search/:term',{term: '@term'},{ getAll : { method : 'get', //isArray : true, params : { results : ':results', appId : nutritionixConst.appId, appKey :nutritionixConst.appKey, // brand_id:'513fbc1283aa2dc80c00001f', fields : ':fields', } } }); return { /** * we can specify the params, the query string and the default fields * to turn in the query along with the result size */ getAll : function(_params) { var defaultFields = 'brand_id,item_name,item_id,brand_name,nf_calories,nf_total_fat'; if (!_params.fields) { _params.fields = defaultFields; } return aSearchObject.getAll(_params); } }
})
/**
*
*/ .factory('DataServiceHTTP', function( $http, nutritionixConst){ return { getAll : function(_key) { return $http.get('https://api.nutritionix.com/v1_1/search/' + _key,{ 'params' : { results : '0:50', appId : nutritionixConst.appId, appKey :nutritionixConst.appKey, // brand_id:'513fbc1283aa2dc80c00001f', fields : 'brand_id,item_name,item_id,brand_name,nf_calories,nf_total_fat' } }); } }
}) .factory('Weather', function($resource) { var API_PATH = 'http://api.openweathermap.org/data/2.5/weather'; var Weather = $resource(API_PATH); return { getWeather: function(weatherParams) { return Weather.get(weatherParams, function(successResult) { return successResult; }, function(errorResult) { console.log(errorResult); }); } }
}) .config(function($stateProvider, $urlRouterProvider) { $stateProvider .state('index', { url: '/', templateUrl: 'home.html', controller : 'HomeCtrl' }); $urlRouterProvider.otherwise("/");
});
Developer | Aaron K Saunders |
Username | aaronksaunders |
Uploaded | September 08, 2022 |
Rating | 3.5 |
Size | 3,587 Kb |
Views | 32,384 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
A Pen by aaron k saunders | 2,916 Kb |
Nested ng-repeat in AngularJS with Checkboxes | 2,600 Kb |
Promise sample for Cross-Platform mobile application class | 2,214 Kb |
Using ui-router to control application flow | 3,448 Kb |
Angular2 Http Example | 3,851 Kb |
A Pen by aaron k saunders | 4,143 Kb |
Demo ui-router basic state change | 2,042 Kb |
A Pen by aaron k saunders | 2,286 Kb |
Firebase Login Example | 2,682 Kb |
Leaflet Map Directive in Ionic | 2,517 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Minimelephant | Smashlee | 2,507 Kb |
My three.js practice | Esambino | 3,203 Kb |
Review test | Otro_user_gil | 4,054 Kb |
Twitch API | Coderpilot | 3,412 Kb |
Css3 slide | Nakome | 3,190 Kb |
Vue.js Lazy Loading | Kjbrum | 3,620 Kb |
HTM5 picture dropzone | Jaysalvat | 2,576 Kb |
Mosaic transition effect between two photos using jQuery | Stathisg | 2,518 Kb |
Em Test | Rodesco | 1,784 Kb |
Pagepilling.js | Blossk | 6,554 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!