AngularJS入门之简单的项目搭建:
AngularJS 是一个 JavaScript 框架。AngularJS 通过 指令 扩展了 HTML.且通过 表达式 实现页面与js数据的双向绑定。主体采用开发人员非常熟识的MVC(model、view、controller)模式,采用路由(route)模式实现单页面跳转(Ps:AngularJS 模块的 config 函数用于配置路由规则)。AngularJS有着诸多特性,最为核心的是:MVC、模块化、自动化双向数据绑定、语义化标签、依赖注入等等。
详情请查阅:https://baike.baidu.com/item/AngularJS/7140293?fr=aladdin
angular.js 版本下载: https://github.com/angular/angular.js/releases
本文所采用开发工具WebStrom下载:http://www.jetbrains.com/webstorm/
为了减少本文赘述请查阅:https://www.kafan.cn/edu/560216.html
PS:如有使用eclipse的开发人员可:
<html ng-app="myApp">
<head>
<script src="js/lib/angular.min.js">script>
<script src="js/lib/angular-route.js">script>
<script src="js/utils/url.js">script>
<script src="js/utils/selfmd5.js">script>
<script src="js/utils/toast.js">script>
<script type="text/javascript" src="js/lib/jquery.min.js">script>
<script src="js/controller/indexController.js">script>
<script src="js/controller/homeController.js">script>
<script src="js/service/HttpService.js">script>
head>
<body ng-controller="indexController">
<div>
<label>Name:label>
<input type="text" ng-model="yourName" placeholder="Enter a name here">
<hr>
<h1>Hello {{yourName}}!h1>
<div ng-view>
div>
div>
body>
html>
/**
* Created by Administrator on 2017/12/1.
*/
//应用程序声明以及将controller、service等放入容器中(PS:类似于SpringMvc的IOC容器)
var myApp = angular.module('myApp',["ngRoute",'homeController','httpService']);
//控制器声明
myApp.controller('indexController', ['$scope',function($scope) {
//数据绑定
$scope.yourName= 'Hola!';
//ng-view监听
$scope.$on("$viewContentLoaded",function(){
console.log("ng-view content loaded!");
});
$scope.$on("$routeChangeStart",function(event,next,current){
//event.preventDefault(); //cancel url change
console.log("route change start!");
});
}]);
//模块的 config 函数用于配置路由规则
myApp.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/home', {
templateUrl: 'content/homepage.html',
//如果路由对页面指明了controller,页面无需使用ng-controller="homeController"声明,否则将会 //出现homeController多次调用
controller: 'homeController'
})
.otherwise({
redirectTo: '/home'
});
});
<html>
<head lang="en">
<meta charset="UTF-8">
<title>SingleCasetitle>
head>
<body >
<input ng-model="user.account"/>
<input ng-model="user.pwd"/>
<input type="button" ng-click="login()">
<ul ng-repeat="menu in menus">
<li ng-click="menu_2Show(menu.lid)" >{{menu.name}}
<ul ng-repeat="menu_list in menu.menu_list" style="display: none" id="menu_2{{menu.lid}}">
<li ng-click="menu_2Click(menu_list.name)"> {{menu_list.name}}li>
ul>
li>
ul>
body>
html>
/**
* Created by Administrator on 2017/12/1.
*/
angular.module("homeController",["ngRoute"])
.controller("homeController",function($scope,$route,httpService,$location){
$scope.user={account:'',password:'',pwd:''};
$scope.user.password = hex_md5($scope.user.pwd);
$scope.login=function(){
httpService.postReq( $scope.user,'/loginbusiness/login','用户登陆!').then(function(data){
if(data.code>0){
//controller中进行路由跳转
$location.path("/home");
}else{
//Android效果toast
alerToast(data.code+':'+data.msg,2000);
}
});
};
$scope.role={role:0};
$scope.menus=null;
httpService.postReq( $scope.role,'','用户登陆!').then(function(data){
$scope.menus=data.data;
});
$scope.menu_2Click=function(name){
alerToast(name,2000);
}
$scope.menu_2Show=function(lid){
$('#menu_2'+lid).show();
}
});
/**
* Created by Administrator on 2017/9/28.
*/
angular.module("httpService",[])
//请求头设置 PS:可写 拦截器 全局$http注入loading效果
.config(["$httpProvider", function ($httpProvider) {
//更改 Content-Type
$httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8";
$httpProvider.defaults.headers.post["Accept"] = "*/*";
$httpProvider.defaults.transformRequest = function (data) {
//把JSON数据转换成字符串形式
if (data !== undefined) {
return $.param(data);
}
return data;
};
}])
.service('httpService', function( $http,$q) {
//生成deferred异步对象
var deferred=$q.defer();
this.postReq = function(reqdata,api,msg) {
console.log(msg+":start!");
$http({
method: "POST",
url: lh_business_url+api,
headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
data:reqdata
}).success(function(data, status) {
console.log(data);
//执行到这里时,改变deferred状态为执行成功,返回data为从后台取到的数据,可以继续执行then,done
deferred.resolve( data);
}).
error(function(data, status) {
alert('连接服务器出错!');
//执行到这里时,改变deferred状态为执行失败,返回data为报错,可以继续执行fail
// deferred.reject('连接服务器出错!');
});
console.log(msg+":end!");
//起到保护作用,不允许函数外部改变函数内的deferred状态
return deferred.promise;
};
this.getReq = function(data,api,msg) {
console.log(msg+":start!");
$http.get(lh_business_url+api, {
params:data
}).success(function (data) {
console.log(data);
// 如果连接成功,延时返回给调用者
deferred.resolve(data);
}) .error(function () {
alert('连接服务器出错!');
// deferred.reject('连接服务器出错!');
});
console.log(msg+":end!");
return deferred.promise;
};
});
效果展示
资源地址AnglarjsDemo