gpt4 book ai didi

javascript - 如何在angular js中获取到 Controller 的路由值

转载 作者:行者123 更新时间:2023-11-30 16:52:50 25 4
gpt4 key购买 nike

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

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**

});

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

最佳答案

您需要使用 routeParams

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

关于javascript - 如何在angular js中获取到 Controller 的路由值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30234230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com