2016-08-11 7 views
0

こんにちは、私は初心者で、angular.jsを学んでいます。私は実際にはAPI(http://ba.dev/businesses/businesspage/36)ですが、私はどのように私のcontroller.jsファイルでこのことを書くことができます知っていないこのURLからデータを取得したい。私のコードを教えてください私はどのようにid = 36を持つビジネスからのデータを取得することができます私を説明してください。ここで

var reControllers = angular.module('reControllers',[]); 
reControllers.controller('RecentController', ['$scope','$http' ,function($scope,$http){ 
    $http.get('http://ba.dev/businesses/businesses').success(function(data){ 
     $scope.business = data; 
    }); 
}]); 

reControllers.controller('PageController', ['$scope','$http', '$routeParams' ,function($scope,$http,$routeParams){ 
    $http.get('http://ba.dev/businesses/businesspage').success(function(data){ 
     $scope.student = data; 
     $scope.whichItem = $routeParams.itemId; 
    }); 
}]); 

私app.jsが

var myApp = angular.module('myApp',[ 
    'ngRoute',         
    'reControllers'     
]); 

myApp.config(['$routeProvider', function($routeProvider){ 
    $routeProvider.      
     when('/main',{    
     templateUrl : 'partials/main.html', 
     controller : 'RecentController' 
    }). 
    when('/page/:itemId',{ 
     templateUrl : 'partials/page.html', 
     controller : 'PageController' 
    }). 
    otherwise({     
     redirectTo: '/main' 
    }); 
}]);  

答えて

1

ファイルであるあなたは、その後実行、URLからitemIdを渡される、サーバー側のロジックを追加$routeParams

$http.get('http://ba.dev/businesses/businesspage/'+ $routeParams.itemId) 

を使用してURLからIDを取得することができますDBからデータを取得して戻すために必要な操作。

0

successの代わりにthenを使用する必要があります。これは推奨されていないためです。だから、あなたのコードは次のようになります。 `

$http.get('http://ba.dev/businesses/businesses').then(function(data){ 
$scope.business = data; 
}); }]);..... 

AngularJSのドキュメントから:ルートのparamsについて

// Simple GET request example: 
$http({ 
    method: 'GET', 
    url: '/someUrl' 
}).then(function successCallback(response) { 
    // 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. 
    }); 

さらに詳しい情報:AngularJS $routeParams

+0

場合であっても、人々は' SUCCESS'と 'エラーを使用しないでください'(あなたの先端は正しいですが)、それは質問に答えません。実際、それは仮定の答えの補数でなければなりません。 – developer033

関連する問題