1

I want to pass a value from one element, to another html page.

Very simply- I want to click an object (within an ng-repeat), and be directly to a page with more detail about that object only.

  1. Take a value (product_id) from the $http getUrl (a value not a directive in my html- can javascript still find it?). Pass this value so it can be accessed if "more info" is requested.

  2. Using a value from take the current product_id, and use that number to fill a getURL to pull a json object for the following "detail" page.

  3. Using ui-sref it opens a new page (not a new URL address, just a different HTML document)

  4. In this new page it should have the product details

This is my my attempt:

.factory('cardsApi', ['$http', function ($http) {
    var apiUrl = 'http://stashdapp-t51va1o0.cloudapp.net/api/item/';

    function productId(min, max) {
        return Math.floor(Math.random() * (max - min + 1) + min);
    }

    var getApiData = function () {
        return $http.get(apiUrl + productId(1, 50000))
    };

    var postRecordLikes = function (product_id) {
        return $http.post('http://test.com/analytic/' + product_id);
    }

    return {
        getApiData: getApiData,
        postRecordLikes: postRecordLikes
    };
}])

.controller('CardsCtrl', ['$scope', 'TDCardDelegate', 'cardsApi', '$http',
    function ($scope, TDCardDelegate, cardsApi, $http) {
        console.log('CARDS CTRL');
        $scope.cards = [];

        $scope.onSwipeRight = function (product_id) {
            console.log(product_id)
        }


        $scope.onSwipeLeft = function (product_id) {
            console.log(product_id)
        }


        // <======  Rewrite with accounts preferences
        for (var i = 0; i < 7; i++) {
            cardsApi.getApiData()
                .then(function (result) {
                    //console.log(result.data) //Shows log of API incoming
                    $scope.cards.unshift(result.data);
                    $scope.product_id = result.data.product_id;
                })
                .catch(function (err) {
                    $log.error(err);
                });
        }
        // Rewrite with accounts preferences =====>

        $scope.$watchCollection('cards', function (newVal, oldVal) {
            if (newVal < oldVal) {
                cardsApi.getApiData()
                    .then(function (result) {

                        // console.log(JSON.stringify(result.data)); Shows log of API results

                        $scope.cards.unshift(result.data);

                        // console.log($scope.cards);

                    })
                //.catch(function (err) {
                //    console.log(err);
                //});
            }
        });

        $scope.cardSwiped = function (card) {
            console.log(card);
            postRecordLikes(card);
        };

        //$scope.cards = Array.prototype.slice.call(cardTypes, 0);

        //Removes card from top of stack
        $scope.cardDestroyed = function (index) {
            $scope.cards.splice(index, 1);
        };

        $scope.addCard = function () {
            var newCard = $scope.cards[$scope.cards.length];
            //newCard.id = Math.random();
            $scope.cards.push(angular.extend({}, newCard));
        };

        var postRecordLikes = function (product_id) {
            cardsApi.postRecordLikes(product_id)
                .then(function successCallback(product_id) {
                    // this callback will be called asynchronously
                    // when the response is available
                }, function errorCallback(response) {
                    // called asynchronously if an error occurs
                    // or server returns response with an error status.
                });
        };

    }
])


.controller('ProductsCtrl', ['$scope', 'TDCardDelegate', 'productApi', '$http',
    function ($scope, TDCardDelegate, cardsApi, $http) {
        console.log('PRODUCTS CTRL');
    }
])


.factory('productApi', ['$http',
    function($http) {
        var apiUrl = 'http://stashdapp-t51va1o0.cloudapp.net/api/item/' + product_id;

        var getApiData = function() {
            return $http.get(apiUrl)
        };

        return {
            getApiData: getApiData
        };
    }]
)

My routing.js (trying to configure it to direct to any URL containing integers/numbers). This always redirects back to login...: c .config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {

        $urlRouterProvider.otherwise("/login");

        $stateProvider

            .state('login', {
                url: '/login',
                views: {
                    'menuContent': {
                        templateUrl: 'app/core/login/login.html'
                    }
                },
                controller: function ($ionicHistory, $scope) {
                    console.log('Clearing history!');
                    $ionicHistory.nextViewOptions({
                        historyRoot: true,
                        disableBack: true
                    });
                }


            })

            .state('product', {
                url: '/product',
                when:('/product/?:product_id'),
                views: {
                    'menuContent': {
                        templateUrl: 'app/components/product/product.html'
                    }
                }


            })
Ycon
  • 1,830
  • 3
  • 27
  • 56
  • 1
    you mean like a query string? – lucas Dec 21 '15 at 12:58
  • I'm not sure what a query string is. Some to pass something to the other page so it knows which value to get. I've seen this down with URL's (splicing the end of the address) but I do not know how to do this. – Ycon Dec 21 '15 at 13:05

1 Answers1

0

a query string adds data to the end of a url, after a question mark. you then navigate to that url:

var myvar=234;
location="http://www.example.com/?"+myvar;

and in the second page get the variable by accessing the query string and stripping away the question mark:

var newvar=location.search.replace("?", ""); // 234

you would then use the 234 to make the specific ajax call, get the JSON, etc, etc

lucas
  • 1,485
  • 13
  • 22
  • I tried this, my routing doesnt like the URL's. Will this work with angular JS? – Ycon Dec 21 '15 at 13:25
  • 1
    using query strings when switching routes: http://stackoverflow.com/a/25899932/1377176 – lucas Dec 21 '15 at 13:40
  • Thanks but I do not udnerstand that. Can you give me an example? I made my code into a plunker http://plnkr.co/edit/R4L89zii3txxtkWOZkOn?p=preview – Ycon Dec 21 '15 at 13:44