Linking external json file , factory, controller and view


#1

Hi,

New to ionic. I have successfully linked a dummy array of json data that is hardcoded into a service.js file. I can generate a list from this data then display data on the subsequent detail view.

I am now trying to achieve the same result but replacing the hardcoded array with reading from a json file (sitting locally). I can log the array to the console and confirm the data exists, but cannot get the data to appear in the list view - view appears blank. Have read multiple posts and web sites but have not been able to progress.

Thanks for any insights.

service.js

angular.module(‘ivfCodes.services’, [])

.factory(‘CodeStore’, function ($http) {

var codes = [];
return {
    list: function () {
        return $http.get("js/data.json").then(function (data) {
            console.log('Success', data);
            codes = data;
            return data;
        });
    },
    get: function (codeId) {
        for (var i = 0; i < codes.length; i++) {
            if (codes[i].id === codeId) {
                return codes[i];
            }
        }
        return undefined;
    }
};

});

controller.js

ivfCodes.controller(‘codesCtrl’, function ($scope, CodeStore) {

$scope.codes = CodeStore.list();

});

view code

<!-- End subheader -->
<ion-content class="has-subheader padding">
    <ion-list>
        <ion-item class="item-remove-animate item-icon-right" ng-repeat="code in codes | filter: query" type="item-text-wrap" href="#/tab/codes/{{code.id}}">
            <h2>{{ code.drugName }} {{ code.strength }}</h2>
            <i class="icon ion-chevron-right icon-accessory"></i>
        </ion-item>
    </ion-list>
</ion-content>

json is of the format:

[
{
“id”: “001”,
“drugName”: “Gonal F”,
“strength”: “900”,
“pbsCode”: “1234A”,
“streamline”: “9876Y”,
“Qty”: “5”,
“Repeats”: “0”,
“Criteria”: “This is some text about the drug requirements.”
},


#2

Solved…finally.

For those with similar issues this post was the most helpful: JSON list on master-page, details on detail-page

In short, I was missing the additional function in the controller.