AngularJs的provider里面的装饰器decorator

我们经常在使用某些Service的时候,更希望它能具备一些额外的功能,这时我们难道改这个Service吗?如果是系统自带的呢,改吗?这当然不现实吧。所以我们的装饰器decorator就发挥作用了,它能让已有的功能锦上添花。我们在config里面使用装饰器。 使用方法: 第一个参数:需要装饰的Service名 第二个参数:一个接受$delegate的回调函数,$delegate代表我们的原来的service实例。 需要注意的是constant常量是不可以被装饰的。 示例代码如下: var app = angular.module(“myApp”, []); app.controller(‘myCtrl’, [‘$scope’, ‘myInfo’, function($scope, myInfo) { console.log(myInfo); }]); app.config([‘$provide’, function($provide) { $provide.decorator(‘myInfo’, function($delegate) { $delegate.lastName = “Prompt”; return $delegate; }) }]); app.service(‘myInfo’, function() { this.fistName = “Shadow”; }) 原来的服务myInfo只有firstName一个属性,我们利用decorator后在不修改myInfo代码的情况下里面添加了lastName属性。

浅析angular.identity

angular.identity这个函数比较简单,官方文档也就简单的交代了下。 使用方法:当你用函数风格书写时,它返回的是函数的一个参数。 function transformer(transformationFn, value) { return (transformationFn || angular.identity)(value); }; 需要额外说明的是:这里面的transformationFn是第一个参数,它将把value作为自己的参数来执行。如果transformation传递的是null或者undefined的话,angular.identity把value作为自己的参数来执行。 示例如下: var app = angular.module(‘myApp’, []); app.controller(‘myCtrl’, [‘$scope’, function($scope) { $scope.result = “”; $scope.input = “”; $scope.double = function(n) { return n * 2; } $scope.answer = function(fn, val) { return (fn || angular.identity)(val); } $scope.cal = function() Read more…

AngularJS加载方式和angular.bootstrap

我们一般写AngularJS时,会在html或者body、div、form等标签里面加上ng-app=”myApp”之类的,这实际上就是让angular自动加载,Angular会自动找到ng-app指令,并将写有ng-app的HTML元素里面的内容作为自己的管辖范围,也就是以该元素为根。这时系统只会找第一个ng-app,如果你的代码里面有多个ng-app的话,angular只理会第一个,后面的直接无视。 如果你的代码里面没有包含ng-app,你就只能手动加载angular了,就需要用到angular.bootstrap这个函数了。 angular.bootstrap函数有两个参数,第一个参数必须,第二个为可选参数。 **使用方法: element(DOM元素):该DOM元素将作为angular的根 modules(数组,数组里面的元素可以是字符、函数、数组):angular需要加载的各个模块的名字所构成的数组 返回值: 为应用新建一个注入器。 示例代码如下: var app = angular.module(“myApp”, []); var app2 = angular.module(“myApp2”, []); var app3 = angular.module(“myApp3”, []); console.log(app); app.controller(‘myCtrl’, [‘$scope’, function($scope) { $scope.test = “This is a test!” }]); app2.controller(‘myCtrl2’, [‘$scope’, function($scope) { $scope.test = “A test too!” }]); app3.controller(‘myCtrl3’, [‘$scope’, function($scope) { Read more…

浅析angular.bind

简单来说angular.bind函数是用来返回一个自己设定参数的函数。它有三个参数,其中第三个参数是可选的。第二个参数是被绑定的函数fn,第一个参数是第二个参数fn的上下文对象,用this调用。 使用方法: self(对象):fn的上下文对象,可用this调用 fn(函数):被绑定的函数 arg(*):可以用来绑定给fn的参数 返回值: 被绑定参数的函数 举个例子 [code lang=”javascript”] var self = {name:’Jack’}; //示例1–带参数 var f = angular.bind(self, //绑定对象,作为函数的上下文 //被绑定的函数 function(age){ console.log(this.name + ‘ is ‘ + age + ‘ !’); }, //绑定的参数,可省略 ’15’ ); //示例2–不带参数 var m = angular.bind(self, //绑定对象,作为函数的上下文 //被绑定的函数 function(age, sex){ console.log(this.name + ‘ is ‘ Read more…

浅析angular.forEach

这个forEach还是比较简单的,angular.forEach有三个参数,前两个参数是必须的,第三个是可选的; 使用方法: 名称 说明 obj(对象或者数组) 被迭代对象 iterator(函数) 迭代器函数 context(对象) 在iterator中被指定为上下文的对象,也就是说在iterator里面的this指的就是此处的context 返回值 (对象或者数组) 返回的是前面第一个参数obj的引用。 举个例子 var values = {name: ‘misko’, gender: ‘male’}; var log = []; angular.forEach(values, function(arg1, arg2, arg3) { this.push(arg1 + ‘: ‘ + arg2); console.log(arg3); }, log); console.log(log); 这里forEach传递了三个参数,先说说里面的第三个参数log,将forEach的上下文指定为log,这样在第二个参数function里面的this指的就是log,如果不填写第三个参数的话,里面的this指向的就是window。 还要说明的是这里面的第二个参数function,function里面最多可以填写三个参数: 参数 说明 arg1就是我们常见的key 即此处的name/gender arg2就是我们常见的value 即此处的misko/male,一般不用 第三个参数arg3 如果非要第三个参数的话,arg3的值就是此处的对象values。

浅析AngularJS中的$interpolate

$interpolate服务是一个可以接受三个参数的函数,其中第一个参数是必需的。 使用方法: |text(字符串)|一个包含字符插值标记的字符串。| |—-|—-| |mustHaveExpression(布尔型)|如果将这个参数设为true,当传入的字符串中不含有表达式时返回null,而不是我们期望的interpolation function。| |trustedContext(字符串)|AngularJS会对已经进行过字符串插值操作的字符串通过$sec.getTrusted()方法进行严格的上下文转义。| |返回值(函数)|用来在特定的上下文中运算表达式。| 假设我们在群发邮件的时候,邮件内容一样,只是收件人’to’不同,或者想让不同的发件人’from’也不同,这里我们可以将邮件内容’emailBody’写成模板,将to和from写成“变量”: emailBody = ‘Hello {{ to }}, Best wishes from {{from}}’ 要在字符串模板中做插值操作,需要在你的对象中注入$interpolate服务。在下面的例子中,我们将会将它注入到一个控制器中 var app = angular.module(“myApp”, []); app.controller(‘MyController’, function($scope, $interpolate) { $scope.to = ‘ari@fullstack.io’; $scope.from = ‘123456@qq.com’; $scope.emailBody = ‘Hello {{ to }},\nBest wishes from {{from}}’; //Set up a watch $scope.$watch(‘to’, function(body) Read more…

自用Angularjs开发环境搭建(国内通用)

搭建VPS 本人用的是搬瓦工的$9.99/y套装,可以用软件putty或者官网kiwivm控制台的里面的Root Shell Interactive连接主机, 搭建shadowsocks环境 按照如下步骤: wget –no-check-certificate https://raw.githubusercontent.com/teddysun/shadowsocks_install/master/shadowsocks.sh chmod +x shadowsocks.sh ./shadowsocks.sh 2>&1 | tee shadowsocks.log 安装完成后会出现 Congratulations, shadowsocks install completed! Your Server IP:your_server_ip Your Server Port:8989 Your Password:your_password Your Local IP:127.0.0.1 Your Local Port:1080 Your Encryption Method:aes-256-cfb Welcome to visit:http://teddysun.com/342.html Enjoy it! 操作shadowsocks的相关指令: 启动:/etc/init.d/shadowsocks start 停止:/etc/init.d/shadowsocks stop 重启:/etc/init.d/shadowsocks Read more…

《 AngularJS深度剖析与最佳实践》安装front-jet失败

作者雪狼已经给出了解决方案,地址估计学习这个的读者应该都能翻过去吧。 本人在此也写一下自己的安装经历吧。 我一开始买到书后就想安装front-jet,始终没有成功,之后再github上找源码无意间发现作者给出来解决方案。 一些读者反映安装FrontJet有困难,我做了一些改进,它支持在Node 5下运行。由于Node 5.x 版本的NPM工具有了显著提升,所以安装到一半儿被卡住等问题都解决了。请获取最新版FrontJet源码,并在它的目录下运行npm link。但是仍然需要翻墙,因为phantomjs的下载源被墙了。 部分书友反映安装front-jet失败,这是因为fj依赖了很多第三方库,安装时容易出现问题,特别是网络不好的时候。所以我制作了Windows下的离线安装包,链接: http://pan.baidu.com/s/1mgZ3FMK,密码: q5v5。先确保本地的NodeJS是4.x版本(注意:离线安装包不支持其它版本的NodeJS),然后把它解压到一个目录,然后把这个目录加入环境变量PATH中即可 —— 注意,添加完PATH之后要重新开cmd窗口才会生效。 如果安装后运行时出现The ‘libsass’ binding was not found错误,请进入node_modules/gulp-sass目录,然后运行npm rebuild node-sass即可。 1、我就在家里的Win64位的笔记本上安装了nodejs4.4.7的64位版本,并且下载了百度云里面的front-jet离线包; 2、安装完成后进入front-jet所在目录,运行 fj create BookForum,再进入BookForum文件夹运行fj serve时提示错误,就是作者提到的“The ‘libsass’ binding was not found错误”; 3、此时安装作者给的解决方案:“请进入node_modules/gulp-sass目录,然后运行npm rebuild node-sass即可”,搞定此问题。 4、运行fj serve还是出错,但是此时的错误就比较明显了,会有指引性提示,大致意思就是要你手动运行bower install。 5、我就下载了bower和git(书中已经提到要按照git)在BookForum文件夹内运行bower init,填写了相关信息,然后运行bower install(亲测得先运行bower init,才行)。 6、最后终于大功告成。 我回单位电脑Win10 64位安装同样办法安装front-jet失败,可能跟win10系统有关。 于是我再次尝试直接用npm install -g fj安装,我的nodejs还是尝试离线版时安装的4.4.7的64位版本,期间提示需要下载phantomjs,为了节省时间,我自己下载了phantomjs,并将其添加至PATH环境变量。 再次运行npm Read more…

制作省份城市选择列表

利用dom技术将省份和城市列表项逐一输出,并且假设设置的有默认省份和城市,能直接选中。 <!DOCTYPE html> <html lang=”en”> <head> <meta charset=”UTF-8″> <title>城市列表</title> <script src=”CitysData.js” ></script> </head> <body> <script> function cc(sf, cs) { document.write(‘<select onchange=”select(this)” id=”provinces”>’); document.write(‘<option value=””>–选择省份–</option>’); var a = 0; for (var i = 0; i < arrCity.length; i++) { if (typeof(sf) != “undefined”) { if (arrCity[i][“name”] == sf) { var sel Read more…

PHP版Google People API的坑

最近在学习Google People API,先看的PHP版的,当GET https://people.googleapis.com/v1/people/me 的时候,想指定获取的内容,比如person.names,person.phoneNumbers,谷歌提供的接口是传入一个数组Query parameters, Parameter name Type Description pageToken string The token of the page to be returned. pageSize number The number of connections to include in the response. Valid values are between 1 and 500, inclusive. Defaults to 100. sortOrder enum(SortOrder) The order in which the connections Read more…