Javascript AngularJS将值传递给查询API
我拥有的是要附加到URL以查询API中正确对象的ID值 HTML: 工厂 例如: 在HTML中,我有一个值38,它总是手动输入的。然后,希望获取该值38并创建http://api/v1/tiles/38 返回API上ID为38的对象的数据Javascript AngularJS将值传递给查询API,javascript,angularjs,api,Javascript,Angularjs,Api,我拥有的是要附加到URL以查询API中正确对象的ID值 HTML: 工厂 例如: 在HTML中,我有一个值38,它总是手动输入的。然后,希望获取该值38并创建http://api/v1/tiles/38 返回API上ID为38的对象的数据 我可以得到ID值,但不知道如何传递它并调用API 在使用$resource之前,确实不需要使用它,通过将它包装到get函数中,直接调用它即可: app.controller('appCtrl',['$scope', '$http', 'ImageTiles',
我可以得到ID值,但不知道如何传递它并调用API 在使用$resource之前,确实不需要使用它,通过将它包装到get函数中,直接调用它即可:
app.controller('appCtrl',['$scope', '$http', 'ImageTiles', function($scope, $http, ImageTiles){
$scope.$watch('tileID', function(){
console.log($scope.tileID);
ImageTiles.get({id: $scope.tileID}).$promise.then(function(tile) {
$scope.tile = tile;
});
});
}]);
这似乎只是Angular应用程序的基本框架。要提供实际的路由机制,您必须修改app.js文件以配置不同的路由 请参见以下示例代码:
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when("/Apps", {templateUrl: "partials/App.html", controller: "driversController"}).
when("/App/:id", {templateUrl: "partials/App.html", controller: "appCtrl"}).
otherwise({redirectTo: '/App'});
}]);
我看起来像
// Get
$http.get("http://api/v1/tiles/"+ tileID)
.success(function (response) {
// Do somthing
});
希望它能帮助您在手表中指定参数newValue和oldValue,并使用newValue调用API
app.controller('appCtrl',['$scope', '$http', 'ImageTiles', function($scope, $http, ImageTiles){
$scope.$watch('tileID', function(newValue, oldValue){
console.log(newValue);
ImageTiles.get({id: newValue}, function(data){
console.log(data);
});
});
}]);
问题是,我必须将值传递到工厂。这正是我要寻找的,非常感谢:。这是一个漫长的夜晚,只是无法思考如何获取该值并将其传递给用户。感谢您的回复,这只是使用正确的URL请求点击API并返回对象的问题。
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when("/Apps", {templateUrl: "partials/App.html", controller: "driversController"}).
when("/App/:id", {templateUrl: "partials/App.html", controller: "appCtrl"}).
otherwise({redirectTo: '/App'});
}]);
// Get
$http.get("http://api/v1/tiles/"+ tileID)
.success(function (response) {
// Do somthing
});
app.controller('appCtrl',['$scope', '$http', 'ImageTiles', function($scope, $http, ImageTiles){
$scope.$watch('tileID', function(newValue, oldValue){
console.log(newValue);
ImageTiles.get({id: newValue}, function(data){
console.log(data);
});
});
}]);