Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 角度参数$routeParams和http.get参数方法';不起作用_Angularjs - Fatal编程技术网

Angularjs 角度参数$routeParams和http.get参数方法';不起作用

Angularjs 角度参数$routeParams和http.get参数方法';不起作用,angularjs,Angularjs,我试图建立一个博客网站只用前端。我的博客数据在api中。 我已经使用angularjs get方法构建了博客提要,其中显示了不同博客的列表。但是对于博客的详细视图,我需要一个来自api的博客数据结果。 我已经尝试使用$routeParams选项。但它不起作用。我的博客api具有id参数,该参数返回特定博客id的博客数据。请帮我解决这个问题 blogdetail.html(模板) 网站博客细节部分的路线提供商 $routeProvider .when("/Blog/:id", {

我试图建立一个博客网站只用前端。我的博客数据在api中。 我已经使用angularjs get方法构建了博客提要,其中显示了不同博客的列表。但是对于博客的详细视图,我需要一个来自api的博客数据结果。 我已经尝试使用$routeParams选项。但它不起作用。我的博客api具有id参数,该参数返回特定博客id的博客数据。请帮我解决这个问题

blogdetail.html(模板)

网站博客细节部分的路线提供商

$routeProvider
    .when("/Blog/:id", {
        controller : "blogController" ,
        templateUrl : "views/BlogDetail.html"
    })
这就是我犯的错误-
TypeError:无法读取未定义的属性“id”

您需要在函数之前添加数组中函数中所需的所有参数,否则这些参数未定义

问题就在这里['$scope','$http',function

'use strict';
    app.controller('blogController', ['$scope','$http', '$routeParams', function ($scope, $http, $routeParams) {
        $http.get('http://example.com/api/blog/', {
            params: {id: $routeParams.id}
        })
           .then(function(res){
              $scope.blog = res.data;
            }).catch(function(response) {
            console.log("ERROR:", response);
        });
    }]);

我尝试了包含“routeParams”的解决方案,但错误是-error-[$injector:unpr]$routeParams应该感谢您,您的解决方案确实帮助我获得了博客详细信息视图,还感谢您对问题的及时回复。`['$scope','$http',function($scope,$http,$routeParams)`在这里,您只请求作用域和http,缺少routeParams。这就是它无法传递给函数的原因。附带说明:如果您现在正在构建它,为什么要使用AngularJs?AngularJs早已停止使用,转而支持Angular 2+。我强烈建议尽早切换到Angular:)
$routeProvider
    .when("/Blog/:id", {
        controller : "blogController" ,
        templateUrl : "views/BlogDetail.html"
    })
'use strict';
    app.controller('blogController', ['$scope','$http', '$routeParams', function ($scope, $http, $routeParams) {
        $http.get('http://example.com/api/blog/', {
            params: {id: $routeParams.id}
        })
           .then(function(res){
              $scope.blog = res.data;
            }).catch(function(response) {
            console.log("ERROR:", response);
        });
    }]);