Angular1.4.6框架简单读取数据库信息并渲染完成news新闻文章列表以及detail详情页功能(小试牛刀)

简介: Angular1.4.6框架简单读取数据库信息并渲染完成news新闻文章列表以及detail详情页功能(小试牛刀)

项目结构


css/angular-common.css


table tr td:first-child {

   /**背景图片*/

   width: 200px;

   height: 100px;

   /**居中填满*/

   background-repeat: no-repeat;

   background-position: center;

   background-size: cover;

}

a {

   text-decoration: none;

   cursor: pointer;

   border-bottom: 1px solid transparent;

}

a:hover {

   border-color: black;

}

.hasLink {

   pointer-events: auto;

   color: black;

   border-bottom: 1px solid black;

}

.hasLink:hover {

   color: blue;

   border-bottom: 1px solid blue;

}

.noLink {

   pointer-events: none;

   color: lightgray;

}

._active {

   background-color: red;

}


news.html


<!doctype html>

<html ng-app="app" ng-cloak>

<head>

   <meta charset="UTF-8">

   <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">

   <meta http-equiv="X-UA-Compatible" content="ie=edge">

   <title>Angular-新闻列表页</title>

   <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>

   <script src="js/public/k-app-common.js"></script>

   <script src="js/angular/angular.min.js"></script>

   <link rel="stylesheet" href="css/angular-common.css">

</head>

<body ng-controller="ctrl">

<div>

   <table>

       <tr ng-repeat="item in list">

           <td style="background-image: url({{item.pic}})"></td>

           <td>

               <h2>{{item.title}}</h2>

               <h3>{{item.content | stripHTML}}</h3>

           </td>

           <td>

               <a ng-href="detail.html?id={{item.id}}" target="_blank">查看详情</a>

           </td>

       </tr>

   </table>

</div>

</body>

<script src="js/news.js"></script>

</html>


js/news.js


/**angular post 请求*/

let url = 'http://your_ip:your_port/api/api_name'

const config = {

   headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}, transformRequest: function (data) {

       return $.param(data);

   }

};

let app = angular.module('app', []);

app.controller('ctrl', function ($scope, $http) {

   $http.post(url, {start: 0, count: 6}, config).then((r) => {

       /* 请求成功执行代码*/

       /* alert(JSON.stringify(r.data))*/

       $scope.list = r.data.data

   }, (r) => {

       /* 请求失败执行代码*/

       alert(JSON.stringify(r))

   });

});

app.filter('stripHTML', function() {

   //可以注入依赖

   return function(c) {

       return c.stripHTML().substring(0, 30) + "...";

   }

});


detail.html


<!doctype html>

<html ng-app="app" ng-cloak>

<head>

   <meta charset="UTF-8">

   <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">

   <meta http-equiv="X-UA-Compatible" content="ie=edge">

   <title>Angular-详情页</title>

   <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>

   <script src="js/public/k-app-common.js"></script>

   <script src="js/angular/angular.min.js"></script>

   <script src="js/angular/angular-sanitize.min.js"></script><!--奇葩的angular,用个ng-bind-html还需要依赖angular-sanitize.min.js,真是日了狗了!!!-->

   <link rel="stylesheet" href="css/angular-common.css">

</head>

<body ng-controller="ctrl">

<div>

   <div>

       <h1>{{data.title}}</h1>

       <h6>{{data.date|format}}</h6>

       <p ng-bind-html="data.content"></p><!--如果不引入angular-sanitize.min.js,就会报错-->

   </div>

   <div>

       <a ng-href="{{data.prevHref}}" target="_self" ng-class="data.hasPrevLink?'hasLink':'noLink'">[上一篇] {{data.prevTitle}}</a><br>

       <a ng-href="{{data.nextHref}}" target="_self" ng-class="data.hasNextLink?'hasLink':'noLink'">[下一篇] {{data.nextTitle}}</a><br><br>

   </div>

</div>

</body>

</html>

<script src="js/detail.js"></script>


js/detail.js


let url = 'http://your_ip:your_port/api/api_name'

let detailId = hash.getQueryString("id");

detailId || alert("id参数获取失败");

const config = {

   headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}, transformRequest: function (data) {

       return $.param(data);

   }

};

let app = angular.module('app', ['ngSanitize']);

app.controller('ctrl', function ($scope, $http) {

   $scope.data = {

       title: "加载中…",

       date: "",

       content: "加载中…",

       prevHref: "",

       prevTitle: "文章标题加载中…",

       nextHref: "",

       nextTitle: "文章标题加载中…",

       hasPrevLink: false,

       hasNextLink: false,

   };

   $http.post(url, {id: detailId}, config).then((r) => {

       /* 请求成功执行代码*/

       /* alert(JSON.stringify(r.data))*/

       r = r.data.data;

       document.title = r.title;

       $scope.data.title = r.title;

       $scope.data.date = r.date;

       $scope.data.content = r.content;

   }, (r) => {

       /* 请求失败执行代码*/

       alert(JSON.stringify(r))

   });

   $http.post(url, {}, config).then((r) => {

       /* 请求成功执行代码*/

       let arr = r.data.data;

       let len = arr.length;

       let id = parseInt(detailId);

       let index = parseInt(array.getIndexById(arr, id));

       if (index <= 0) {

           $scope.data.prevHref = "#";

           $scope.data.prevTitle = "没有了";

           $scope.data.hasPrevLink = false;

       } else {

           let _index_l = index - 1;

           $scope.data.prevTitle = arr[_index_l].title;

           $scope.data.prevHref = "detail.html?id=" + arr[_index_l].id;

           $scope.data.hasPrevLink = true;

       }

       if (index >= len - 1) {

           $scope.data.nextHref = "#";

           $scope.data.nextTitle = "没有了";

           $scope.data.hasNextLink = false;

       } else {

           let _index_n = index + 1;

           $scope.data.nextTitle = arr[_index_n].title;

           $scope.data.nextHref = "detail.html?id=" + arr[_index_n].id;

           $scope.data.hasNextLink = true;

       }

   }, function () {

       alert('接口请求失败!');

   });

});

app.filter('format', function () {

   //可以注入依赖

   return function (c) {

       if (c == "") return;

       return date.formatDateTime(c);

   }

});

相关文章
|
SQL 关系型数据库 MySQL
阿里云RDS云数据库全解析:产品功能、收费标准与活动参考
与云服务器ECS一样,关系型数据库RDS也是很多用户上云必买的热门云产品之一,阿里云的云数据库RDS主要包含RDS MySQL、RDS SQL Server、RDS PostgreSQL、RDS MariaDB等几个关系型数据库,并且提供了容灾、备份、恢复、监控、迁移等方面的全套解决方案,帮助您解决数据库运维的烦恼。本文为大家介绍阿里云的云数据库 RDS主要产品及计费方式、收费标准以及活动等相关情况,以供参考。
|
SQL 存储 关系型数据库
MySQL功能模块探秘:数据库世界的奇妙之旅
]带你轻松愉快地探索MySQL 8.4.5的核心功能模块,从SQL引擎到存储引擎,从复制机制到插件系统,让你在欢声笑语中掌握数据库的精髓!
489 26
|
安全 Java 数据库
Jasypt加密数据库配置信息
本文介绍了使用 Jasypt 对配置文件中的公网数据库认证信息进行加密的方法,以提升系统安全性。主要内容包括:1. 背景介绍;2. 前期准备,如依赖导入及版本选择;3. 生成密钥并实现加解密测试;4. 在配置文件中应用加密后的密码,并通过测试接口验证解密结果。确保密码安全的同时,保障系统的正常运行。
1100 3
Jasypt加密数据库配置信息
|
存储 缓存 自然语言处理
评论功能开发全解析:从数据库设计到多语言实现-优雅草卓伊凡
评论功能开发全解析:从数据库设计到多语言实现-优雅草卓伊凡
570 8
评论功能开发全解析:从数据库设计到多语言实现-优雅草卓伊凡
|
SQL Linux 数据库
【YashanDB知识库】崖山数据库Outline功能验证
本文来自YashanDB官网,主要测试了数据库优化器在不同场景下优先使用outline计划的功能。测试环境包括相同版本新增数据、绑定参数执行、单机主备架构以及数据库版本升级等场景。通过创建表、插入数据、收集统计信息和创建outline等步骤,验证了在各种情况下优化器均能优先采用存储的outline计划。测试结果表明,即使统计信息失效或数据库版本升级,outline功能依然稳定有效,确保查询计划的一致性和性能优化。详情可见[原文链接](https://www.yashandb.com/newsinfo/7488286.html?templateId=1718516)。
【YashanDB知识库】崖山数据库Outline功能验证
|
NoSQL 关系型数据库 MongoDB
Apifox与Apipost数据库连接功能详细对比,让接口管理更高效!
在现代软件开发中,数据库是应用运行的核心组件,接口管理工具则是连接和调试数据库的重要桥梁。本文对比了 Apifox 和 Apipost 两款工具的数据库连接功能。Apipost 支持全面的关系型与非关系型数据库(如 MySQL、Redis、MongoDB),功能强大且免费,适合复杂项目;而 Apifox 在关系型数据库支持上表现良好,但非关系型数据库(尤其是 Redis)功能有限且收费,更适合中小项目以关系型数据库为主的需求。根据项目需求选择合适的工具,可显著提升开发效率和稳定性。
|
数据库连接 PHP 数据库
【YashanDB知识库】PHP使用ODBC使用数据库绑定参数功能异常
【YashanDB知识库】PHP使用ODBC使用数据库绑定参数功能异常
|
PHP 数据库
【YashanDB知识库】PHP使用OCI接口使用数据库绑定参数功能异常
【YashanDB知识库】PHP使用OCI接口使用数据库绑定参数功能异常
|
存储 NoSQL 关系型数据库
Apifox与Apipost数据库连接功能详细对比,让接口管理更高效!
Apipost 更加全面:无论是关系型还是非关系型数据库,它都为开发者提供了一站式解决方案,非常适合数据库架构复杂的大型项目。相对来说,Apifox偏重关系型分析和管理:若项目主要需求在于管理关系型数据库,而对非关系型的依赖较小,Apifox倒是可以应付。
472 2
|
SQL 关系型数据库 数据库连接