显示'详细信息'特定对象的-将JSON值从一个页面传递到另一个页面以查询API

Showing 'details' of specific object- pass JSON value from one page to another to query API

本文关键字:一个 另一个 API 查询 对象 详细信息 显示 JSON      更新时间:2023-09-26

我想将一个值从一个元素传递到另一个html页面。

非常简单-我想点击一个对象(在一个ng重复中),并直接进入一个页面,该页面仅包含关于对象的更多详细信息。

  1. 从$http getUrl中获取一个值(product_id)(这个值在我的html中不是指令——javascript还能找到它吗?)。传递此值,以便在请求"更多信息"时可以访问它。

  2. 使用中的值获取当前product_id,并使用该数字填充getURL,为下面的"detail"页面提取json对象。

  3. 使用ui sref打开一个新页面(不是一个新的URL地址,只是一个不同的HTML文档)

  4. 在这个新页面中,它应该有产品详细信息

这是我的尝试:

.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
        };
    }]
)

我的routing.js(试图将其配置为直接指向任何包含整数/数字的URL)。这总是重定向回登录…:c.config(['$stateProvider','$urlRouterProvider',函数($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'
                    }
                }

            })

查询字符串将数据添加到url的末尾,在问号之后。然后导航到那个网址:

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

在第二页中,通过访问查询字符串并去掉问号来获得变量:

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

然后使用234进行特定的ajax调用,获取JSON等