Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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

share|improve this question
up vote 1 down vote accepted

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);
   });
share|improve this answer
    
Thought I was close! I appreciate your help @ManuelObregozo! thanks – Joseph Sjoblom yesterday
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);
        });

}]);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.