发表于: 2017-04-08 23:43:57
1 1084
今天完成的事情:学习angular.module controller ng-if angular的ajix请求 ng-class
明天计划的事情:学习angular路由
遇到的问题: 看了很多教程也跟着敲了一些,但还是用的少,不熟练。
收获:
由于angular和Javascript的数据和函数不互通,为了互通,所以需要使用控制器。
angular.module
ng-app : 的模块,一个ng-app可以有多个模块
写法:
var md = angular.module('test',[]);
angular.module 的第一个参数是ng-app的名字
angular中的控制器,一个module可以有多个控制器
md.controller('cont1',function($scope){
$scope.a = 12;
$scope.alert =function(s) {alert(s);}
});
通过module点操作来访问controller,controller的第一个参数是这个控制器的名字,后面的函数是依赖注入的,
angular的数据都是通过$scope对象来保存的,例如变量和函数。这里就保存了一个变量a和alert函数。
ng-controller :给元素添加控制器,一般给需要控制的元素父级加
<body ng-controller="cont1">
<input type="text" ng-model="str" >
<button ng-click="alert(str)">安妮</button>
用法: ng-controller='控制器的名字',这样就能使用控制器的函数和变量了
ajax,可以这样写:
md.controller('cont1',function($scope,$http){
$http.get('url',{
params: {
act: 'xxx'
}.success(function(rel){
console.log(rel.)
}).error(function(){
//do something
})
})
});
利用$http.get,params中写发送的数据,success是访问成功执行的函数,这里用‘rel’这个变量将返回的数据保存了。error是访问失败执行的函数。
最后,需要记住的是,angular的核心是处理数据,不要去直接操作dom节点,通过数据改变一切。
评论