发表于: 2017-01-12 23:55:13
1 1273
今天完成的事情:
明天计划的事情:
遇到的问题:
收获:
#二师兄分享了,写代码里面的技巧,渐进增强,和逐级递减。这样规范代码的一个概念。我给他32个赞
我猜(见我写不出任务8原型搜索按钮功能),沁姐非常好心的写了一个数据源的流动代码出来让我借鉴
var app = angular.module('app', []);
//获取地址
app.constant('URL', {
studentList: '/student-ajax/students'
});
//一个服务,用于获取接口里面的数据
app.service('studentService', function($http, URL) {
this.getStudentList = function() {
return $http.get(URL.studentList);
};
});
//一个最简单的指令,
app.directive('pages', function() {
return {
//把数据输出到html
restrict: 'EA',
template: '<div>' +
'<p ng-if="scope.req">请求中...</p>' +
'<p ng-if="!scope.req">请求结束</p>' +
'<p>总条数{{total}} 如果每页10条 那么总页数为{{totalPage}}</p></div>',
replace: true,
//进行双向绑定
scope: {
list: '='
},
//link方法传入scope,??
link: function(scope) {
//把scope的list属性
scope.list.then(function(res) {
//新建一个变量来存res的个数
scope.total = res.length;
//
scope.totalPage = Math.ceil(scope.total / 10);
})
}
}
});
//建立一控制器,并且调用服务
app.controller('PageCtrl', function(studentService) {
//新建一个变量等于this
var vm = this;
//变量下面的list属性等于服务的
vm.list = studentService.getStudentList().then(function(res) {
//判断返回成功否
if (res.data.code == 200) {
//成功就把接口的值给res
return res.data.data;
} else {
//不成功就返回空数组
return [];
}
})
});
然而上面这段代码的核心部分我还没有了解透彻。
评论