angular学习(二):Controller定义总结

it2025-06-18  3

上文中总结完了ng-view的应用,将运维后台分开界面到2个,进行到 逻辑Controller处理中,本文将总结一下在项目中Controller都用到了哪些知识:

$scope:作用域对象,仅仅是代表本Controller对象,本作用域的不论什么对象都用$scope来声明。

$rootScope:代表整个页面作用域,能够在随意子$scope之间传递全局对象    

$http:用来发送http请求的对象.

设置header

var module =angular.module('index'); //设置httpheader module.config(function($httpProvider){ $httpProvider.defaults.headers.put['Content-Type'] ='application/json;charset:utf-8'; $httpProvider.defaults.headers.post['Content-Type'] ='application/json;charset:utf-8'; $httpProvider.defaults.headers.put['token']= tokenval; $httpProvider.defaults.headers.post['token'] = tokenval; });

声明一个子变量

$scope.sys_type ='ios';

声明一个function

$scope.editAdv =function(obj) { //some code }

发送HTTP请求

var pa ={"bannerRole":$scope.model_role,"ClientType":$scope.sys_type}; $http.post(commonUrl.adv.findAdv,pa).success(function(data,status, headers, config){ $scope.list = data.body; }).error(function(data, status,headers, config){ alert("error"); })

调用已经声明好的ng方法

$scope.functionName();

总结

              眼下在本人的应用中Controller仅仅用到了以上的知识,基本能够满足CRUD和页面的渲染,之后在用到其它知识在补全吧。

转载于:https://www.cnblogs.com/bhlsheji/p/5136415.html

最新回复(0)