Angularjs练手篇——Server编写
2016-04-19 11:47
537 查看
// 统一初始化
var app = angular.module("app", []);
// 配置常量
app.constant("http", "http://192.168.1.1:0000/webStorm/2016V01/");
app.constant("errorMsg", "服务器异常,请稍后重试");
// 配置http发送模式
app.config(["$httpProvider", function($httpProvider) {
delete $httpProvider.defaults.headers.common["X-Requested-With"];
$httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
$httpProvider.defaults.transformRequest = function(data) {
// 当参数不为空的时候
if (data) {
return $.param(data);
}
};
} ]);
// 配置service
app.service("configService", function($http, http, errorMsg) {
// post获取数据
this.post = function(url, params, success) {
$http.post(http + url, params).success(function(resp) {
if (resp.status) {
success(resp);
} else {
layer.msg(resp.data);
}
}).error(function(resp) {
layer.msg(errorMsg);
});
}
// get获取数据
this.get = function(url, success) {
$http.get(http + url).success(function(resp) {
success(resp);
}).error(function(resp) {
layer.msg(errorMsg);
});
}
//获取地址栏信息
this.getUrlParam = function(name)
{
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r!=null)
return unescape(r[2]);
return null;
}
});
var app = angular.module("app", []);
// 配置常量
app.constant("http", "http://192.168.1.1:0000/webStorm/2016V01/");
app.constant("errorMsg", "服务器异常,请稍后重试");
// 配置http发送模式
app.config(["$httpProvider", function($httpProvider) {
delete $httpProvider.defaults.headers.common["X-Requested-With"];
$httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
$httpProvider.defaults.transformRequest = function(data) {
// 当参数不为空的时候
if (data) {
return $.param(data);
}
};
} ]);
// 配置service
app.service("configService", function($http, http, errorMsg) {
// post获取数据
this.post = function(url, params, success) {
$http.post(http + url, params).success(function(resp) {
if (resp.status) {
success(resp);
} else {
layer.msg(resp.data);
}
}).error(function(resp) {
layer.msg(errorMsg);
});
}
// get获取数据
this.get = function(url, success) {
$http.get(http + url).success(function(resp) {
success(resp);
}).error(function(resp) {
layer.msg(errorMsg);
});
}
//获取地址栏信息
this.getUrlParam = function(name)
{
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r!=null)
return unescape(r[2]);
return null;
}
});
相关文章推荐
- AngularJS双向数据绑定
- angular学习(二)—— Data Binding
- [Angular 2] Child Router
- 基于Angularjs+jasmine+karma的测试驱动开发(TDD)实例
- Angularjs基础(三)
- 前端开发利器: Bootstrap + AngularJS
- angular学习(一)—— 概览
- angularjs 剪贴板
- Introduction to Angular 2 Forms - Template Driven vs Model Driven Forms
- angularjs和angular-cookies 设置cookie的过期时间
- AngularJS入门教程之AngularJS模型
- AngularJS入门教程之AngularJS指令
- AngularJS入门教程之AngularJS表达式
- angularjs 路由 不能嵌套问题
- 创建自己的AngularJS - 作用域和Digest(五)
- [置顶] 跟我学AngularJS:全局变量设置之value vs constant vs rootscope vs 服务
- angular-drag - 基于Angularjs的拖拽指令
- [Angular 2] Router basic and Router Params
- AngularJS最理想开发工具WebStorm
- AngularJS入门教程之AngularJS表达式