1

I'm hoping this is an easy question to answer...

I'm trying to create a table that loads my json, then be able to click a row and load more details that pertain to the json object. When you click a row it should load additional details at the top of the page. The row clicking part is working fine. What I'm having trouble with is loading the initial object by default.

Below is an example of what I'm referring to:

var myItemsApp = angular.module('myItemsApp', [ ]);

myItemsApp.factory('itemsFactory', ['$http', function($http){
    var itemsFactory = {
        itemDetails: function () {
            return $http({
                    url: "fake-phi.json",
                    method: "GET",

            }).then(function (response) {
                return response.data;
            });
        }
    };

    return itemsFactory;

}]);

myItemsApp.controller('ItemsController', ['$scope', 'itemsFactory',         
    function($scope, itemsFactory){
        var promise = itemsFactory.itemDetails();

        promise.then(function (data) {
            $scope.itemDetails = data;
            console.log(data);
        });

        $scope.select = function (item) {
            $scope.selected = item;
        }

}]);

http://embed.plnkr.co/6LfAsaamCPPbe7JNdww1/

I tried adding this after $scope.select, but got an error:

$scope.selected = item[0];

How do I get the first object in my json to load by default?

thanks in advance

2 Answers 2

1

Inside your promise resolve function assign the first item of the array, as a selected value:

   promise.then(function (data) {
        $scope.itemDetails = data;
        $scope.selected = data[0];
        console.log(data);
   });
Sign up to request clarification or add additional context in comments.

1 Comment

Thought I was close! I appreciate your help @ManuelObregozo! thanks
0
var myItemsApp = angular.module('myItemsApp', [ ]);

myItemsApp.factory('itemsFactory', ['$http', function($http){
    var itemsFactory = {
        itemDetails: function () {
            return $http({
                    url: "fake-phi.json",
                    method: "GET",

            }).then(function (response) {
                return response.data;
            });
        }
    };

    return itemsFactory;

}]);

myItemsApp.controller('ItemsController', ['$scope', 'itemsFactory',         
    function($scope, itemsFactory){
        var promise = itemsFactory.itemDetails();

        promise.then(function (data) {
            $scope.itemDetails = data;
            $scope.selected = data[0];

            console.log($scope.itemDetails);
            console.log($scope.selected);
        });

}]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.