简体   繁体   English

如何在angular js中将数据从一页传递到另一页

[英]How to pass data from one page to other page in angular js

I have created a new blog project using angular-js. 我使用angular-js创建了一个新的博客项目。 Here I have one page listing all the blogs and if I click on the edit button or delete button, I want to move to next page and show that data in the form from where I updated the blog. 在这里,我有一页列出所有博客,如果我单击“编辑”按钮或“删除”按钮,我想转到下一页,并以更新博客的形式显示数据。

My blog list page: listblog.html 我的博客列表页面:listblog.html

<!DOCTYPE html>
<html lang="en">

<head>



    <title>My First AngularJs Blog</title>

 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>

    <!-- Bootstrap Core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom CSS -->
    <link href="css/blog-home.css" rel="stylesheet">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

</head>

<body ng-app="blogapp">

    <!-- Navigation -->
   <div ng-include="'includes/header.html'">                    
</div>

    <!-- Page Content -->



       <div class="container">
 <div ng-controller="blogcontroller">
  <h2>Blog List</h2>
  <p>Here is the awsome blog list</p>            
  <table class="table">
    <thead>
      <tr>
        <th>Title</th>
        <th>Blog</th>
        <th>Posted On</th>
        <th>Action</th>
      </tr>
    </thead>
    <tbody>
      <tr  ng-repeat="blg in allblog">
        <td>{{blg.title}}</td>
        <td>{{blg.description}}</td>
        <td>{{blg.created_on }}</td>
        <td><a href="">Edit</a> || <a href="">Delete</a></td>
      </tr>

    </tbody>
  </table>

  </div>
</div>


        <hr>

        <!-- Footer -->

 <div ng-include="'includes/footer.html'">                    
</div>

    <!-- /.container -->

    <!-- jQuery -->
    <script src="js/jquery.js"></script>

    <!-- Bootstrap Core JavaScript -->
    <script src="js/bootstrap.min.js"></script>
<script src="controller.js"></script>
</body>

</html>

I want to create a controller for edit. 我想创建一个控制器进行编辑。 My Controller file: controller.js 我的控制器文件:controller.js

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

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

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


    $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");
    });
  } ;


  $scope.editpost = function(index){

    $http.post('allscript.php?action=edit_post', { 'id' : index})
    .success(function (data, status, headers, config){


    }) 
    .error(function (data, status, headers, config){
           console.log(status);
        });


  }

  });

And my php script page where all operations are performed allscript.php 还有我的php脚本页面,其中执行了所有操作allscript.php

<?php 

$user ="root";
$pass ="m2n1shlko";

$dbh = new PDO('mysql:host=localhost;dbname=blog_db', $user, $pass);


switch($_GET['action']){

    case 'edit_post':
        edit_post();
    break;





function edit_post(){

  $data = json_decode(file_get_contents("php://input"));     
    $index = $data->id; 

$query = $dbh->prepare("SELECT * FROM blog_list where id='$index'") ;
$da = $query->execute();
$getre = $da->fetch(PDO::FETCH_ASSOC);

print_r(json_encode($getre));
return $getre;


}



}
?>

I am new in angular-js. 我是angular-js的新手。 I don't know how to get the ID of that record and to take to the next page for edit/update/delete. 我不知道如何获取该记录的ID,以及如何转到下一页进行编辑/更新/删除。

Any help is appreciated. 任何帮助表示赞赏。 Thanks. 谢谢。

您可以用来在不同页面$window.sessionStorage之间保存数据

Actually the @Jeremy Thille is right... Please check angular ngRoute module for more info https://docs.angularjs.org/tutorial/step_07 . 实际上@Jeremy Thille是正确的...请检查angular ngRoute模块以获取更多信息https://docs.angularjs.org/tutorial/step_07
Also the good practice is to refactor all of your connections/post/get in factories/services, and to inject them directly on your controllers (where you needs of them) http://viralpatel.net/blogs/angularjs-service-factory-tutorial/ 同样好的做法是重构所有连接/发布/获取工厂/服务,并将它们直接注入到控制器中(在需要它们的地方) http://viralpatel.net/blogs/angularjs-service-factory -tutorial /

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用angular js将数据从一页传递到另一页? - How to pass data one page to another page using angular js? 将所选数据从一页传递到html中的另一页 - Pass Selected Data from one page to other page in html 如何使用javascript将json数据从一个html页面传递到另一html页面? - How to pass json data from one html page to other html page using javascript? 如何以角度在单页应用程序中将数据从一页传递到另一页 - How to pass data from one page to another in single page application in angular 如何通过javascript将值从一页传递到另一页,并使用php获取另一页中传递的值? - How to pass values from one page to other page by javascript and get the passed value in the other page using php? 将数据传递给 Angular 中的其他页面/组件 - Pass data to other page/component in Angular 如何使用 VUE js 中的路径在页面之间传递数据? - How to pass data from page to page using path in VUE js? 如何在Vue Js中从一个页面到另一个页面一致地传递数据? - how to pass data consistently from page to page in Vue Js? 如何将数据从一个页面传递到另一个页面html - How to pass data from one page to another page html 如何在html中将数据从一个页面传递到另一个页面? - How to pass data from one page to another page in html?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM