【问题标题】:How to get route value to controller in angular js如何在角度js中获取路由值到控制器
【发布时间】:2015-07-25 21:16:21
【问题描述】:

我是 Angular js 新手,想在博客站点进行简单的 crud 操作,我不知道如何从路由到控制器中获取值以查看要编辑的表单中的特定记录

Controller.js 文件

 var myApp = angular.module("blogapp",[]);

  myApp.config(['$routeProvider',function($routeProvider){

    $routeProvider
      .when('/home',{
        templateUrl:'home.html',
        controller:'blogcontroller'
      })
      .when('/list',{
        templateUrl:'list.html',
        controller:'blogcontroller'

      })
      .when('/add',{

        templateUrl:'add.html',
        controller:'addcontroller'
      })
      .when('/edit/:Blogid',{    **// Want to get this Blogid** 

        templateUrl:'edit.html',
        controller:'editcontroller'
      })
      .otherwise({
        redirectTo:'/home'
      });

  }]);


myApp.controller('blogcontroller',function ($scope,$http){

    $http({method: 'GET' , url: 'getallblog.php'}).success(function(data){
      $scope.allblog = data;
    });

// DELETE blog HERE 
 $scope.removeRow= function(id){



    $http.post("removeblog.php",{'id' : id}).success(function(data,status,headers,config){
      window.location='index.html';
      console.log("Deleted Successfully");

  });
  };

// delete blog code ends here


  });


myApp.controller('addcontroller',function ($scope,$http){





  /// New Post Here
    $scope.new_post =function(){



    $http.post("addblog.php" ,{'title' : $scope.title ,'description' : $scope.description }).success(function(data,status,headers,config){
      window.location='index.html';
      console.log("inserted Successfully");
    });
  };



  // New Post ends Here

});

myApp.controller('editcontroller',function ($scope,$http,$routeParams){

 **// Want to get here this Blogid**

});

如果有人帮助我,不胜感激。谢谢

【问题讨论】:

  • Blogid in route .. 我评论代码检查一下

标签: javascript php angularjs


【解决方案1】:

你需要使用routeParams

myApp.controller('editcontroller',function ($scope,$http,$routeParams){
     $scope.Blogid = $routeParams.Blogid;
})

【讨论】:

  • 好的,我的问题解决了,谢谢...你能告诉我一件事,我想用这个 id 来获取记录,所以我用这个:$http.get('page.php? id='+博客); // 对或错
  • @RahulSaxena 看到这个How to pass data in get.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-05
  • 1970-01-01
  • 2018-10-06
  • 1970-01-01
  • 2016-12-19
  • 1970-01-01
相关资源
最近更新 更多