Javascript 显示';详情';将JSON值从一个页面传递到另一个页面以查询API
我想将一个值从一个元素传递到另一个html页面 非常简单-我想单击一个对象(在ng repeat中),然后直接转到一个只包含该对象更多详细信息的页面Javascript 显示';详情';将JSON值从一个页面传递到另一个页面以查询API,javascript,html,angularjs,Javascript,Html,Angularjs,我想将一个值从一个元素传递到另一个html页面 非常简单-我想单击一个对象(在ng repeat中),然后直接转到一个只包含该对象更多详细信息的页面 从$http getUrl中获取一个值(product_id)(这个值在我的html中不是指令-javascript还能找到它吗?)。传递此值,以便在请求“更多信息”时可以访问它 使用来自的值获取当前产品的id,并使用该数字填充getURL,为下面的“详细信息”页面提取json对象 使用ui sref打开一个新页面(不是一个新的URL地址,只是一个
.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
};
}]
)
查询字符串将数据添加到url末尾的问号后。然后导航到该url:
var myvar=234;
location="http://www.example.com/?"+myvar;
在第二页中,通过访问查询字符串并去掉问号来获取变量:
var newvar=location.search.replace("?", ""); // 234
然后使用234进行特定的ajax调用,获取JSON等查询字符串将数据添加到url末尾的问号后面。然后导航到该url:
var myvar=234;
location="http://www.example.com/?"+myvar;
在第二页中,通过访问查询字符串并去掉问号来获取变量:
var newvar=location.search.replace("?", ""); // 234
然后您将使用234进行特定的ajax调用,获取JSON等您的意思是像查询字符串一样吗?我不确定查询字符串是什么。有些人希望将某些内容传递给另一个页面,以便它知道要获取哪个值。我在URL(地址的末尾拼接)中看到了这一点,但我不知道怎么做。你是说像查询字符串?我不确定查询字符串是什么。有些人希望将某些内容传递给另一个页面,以便它知道要获取哪个值。我已经在URL上看到了这一点(拼接地址的结尾),但我不知道怎么做。我尝试了这个,我的路由不喜欢URL。这是否适用于angular JS?在切换路由时使用查询字符串:谢谢,但我不能忍受。你能给我举个例子吗?我把我的代码做成了一个plunker我试过了,我的路由不喜欢URL。这是否适用于angular JS?在切换路由时使用查询字符串:谢谢,但我不能忍受。你能给我举个例子吗?我把我的代码变成了一个垃圾桶