angularjs基础教程


Posted in Javascript onDecember 25, 2014

很久没有写过东西了,感觉写东西都不知道从哪里开始了,现在还是先写点技术性的吧,angularjs?我兄弟把它叫成“俺哥啦js”

1.下载

官方网址:https://angularjs.org/ 
CDN:https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.5/angular.min.js

2.简单介绍使用 1.ng-app

决定了angularjs的作用域范围,你可以如下使用

<html ng-app> 

…  

</html>

来让angularjs渲染整个页面,也可以使用

<div ng-app='myapp'>

……

</div>

来渲染其中的一部分。

2.ng-model

ng-model,当你的数据模型被改变的时候,譬如ng-model='test',其中这个test的数值被改变的时候,{{test}}的数值也将跟随改变,也就是连接到ng-model中的test也跟随改变,如下

<!doctype html>

<html>

    <head>

        <script src="angular.min.js" type="text/javascript"></script>

        <title>learing argularjs--widuu</title>

    </head>

    <body ng-app>

    <input ng-model='test' >{{test}}

    </body>

</html>

angularjs基础教程

3.angular.module

这个主要是做模块的注册,创建和索引,譬如我们ng-app想把这个注册成为一个服务就要用,当我们引用索引一个模块的时候也要使用

angular.module(name, [requires], [configFn]);

#name       类型string创建的检索模块的名称

#requires   简单理解就是你需要包含的使用模块,譬如ngRoute路由模块

#configFn   可以选配的功能模块,功能和module.config类似

4.controller

controller是angular.Module中的方法controller(name, constructor);其中name是controller的名字,constructor是控制器构造函数,我们利用一段代码来说明

<!doctype html>

<html>

    <head>

        <script src="angular.min.js" type="text/javascript"></script>

        <script type="text/javascript">

        var app = angular.module('myapp',[]);

        app.controller('mytest',function($scope){

            $scope.test="hello word";

        });

        </script>

        <title>learing argularjs--widuu</title>

    </head>

    <body ng-app='myapp' ng-controller='mytest' >

    <input ng-model='test'>{{test}}

    </body>

</html>

angularjs基础教程

5.value

value也是angular.Module中的方法value(name, object);其中name是service的名称,object是服务器实例对象,这个时候我们就可以把上边的代码修改正成这样

<!doctype html>

<html>

    <head>

        <script src="angular.min.js" type="text/javascript"></script>

        <script type="text/javascript">

        var app = angular.module('myapp',[])

        .value('testvalue','word');

        app.controller('mytest',function($scope,testvalue){

            $scope.test="hello "+ testvalue;

        });

        </script>

        <title>learing argularjs--widuu</title>

    </head>

    <body ng-app='myapp' ng-controller='mytest' >

    <input ng-model='test'>{{test}}

    </body>

</html>

5.factory

factory也是angular.Module中的方法factory(name, providerFunction);;其中name是service的名称,providerFunction是函数用于创建新的服务器对象,这个时候我们就可以把上边的代码修改正成这样

<!doctype html>

<html>

    <head>

        <script src="angular.min.js" type="text/javascript"></script>

        <script type="text/javascript">

        var app = angular.module('myapp',[])

            .value('testvalue','widuu')

            .factory('testfactory',function(testvalue){

                return{

                    lable:function(){

                        return "this can output : hello "+ testvalue;

                    }

                }

            });

        app.controller('mytest',function($scope,testvalue,testfactory){

            $scope.test = "hello "+ testvalue;

            $scope.output = testfactory.lable();

        });

        </script>

        <title>learing argularjs--widuu</title>

    </head>

    <body ng-app='myapp' ng-controller='mytest' >

    <input ng-model='test'>{{test}}

    </p>

        {{output}}

    </body>

</html>

angularjs基础教程

6.provider

provider也是angular.Module中的方法provider(name, providerType);其中name是service的名称,providerFunction是函数用于创建新的服务器对象,这个跟factory差不多,我们现在用provider重写

<!doctype html>

<html>

    <head>

        <script src="angular.min.js" type="text/javascript"></script>

        <script type="text/javascript">

        var app = angular.module('myapp',[])

            .value('testvalue','widuu')

            .provider('testprovider',

                function(){

                  this.lable = "this will output : hello widuu";

                  this.$get = function () {

                       return this;

                   }

                }

            );

        app.controller('mytest',function($scope,testvalue,testprovider){

            $scope.test = "hello "+ testvalue;

            $scope.output = testprovider.lable;

        });

        </script>

        <title>learing argularjs--widuu</title>

    </head>

    <body ng-app='myapp' ng-controller='mytest' >

    <input ng-model='test'>{{test}}

    </p>

        {{output}}

    </body>

</html>

7.service

service也是angular.Module中的方法service(name, constructor);其中name是service的名称,constructor一个将被实例化的构造函数,这个跟factory差不多,我们现在用service重写

<!doctype html>

<html>

    <head>

        <script src="angular.min.js" type="text/javascript"></script>

        <script type="text/javascript">

        var app = angular.module('myapp',[])

            .value('testvalue','widuu')

            .service('testservice',

                function(testvalue){

                    this.lable = function(){

                        return "this will output:hello "+testvalue;

                    }

                }

            );

        app.controller('mytest',function($scope,testvalue,testservice){

            $scope.test = "hello "+ testvalue;

            $scope.output = testservice.lable();

        });

        </script>

        <title>learing argularjs--widuu</title>

    </head>

    <body ng-app='myapp' ng-controller='mytest' >

    <input ng-model='test'>{{test}}

    </p>

        {{output}}

    </body>

</html>

8.constant

constant也是angular.Module中的方法constant(name, object);其中name是常量的名称,而object是常量的值,我们可以这样写的

<!doctype html>

<html>

    <head>

        <script src="angular.min.js" type="text/javascript"></script>

        <script type="text/javascript">

        var app = angular.module('myapp',[])

            .value('testvalue','widuu')

            .constant('count',23)

            .service('testservice',

                function(testvalue,count){

                    this.lable = function(){

                        return "this will output:hello "+testvalue+",age is "+count;

                    }

                }

            );

        app.controller('mytest',function($scope,testvalue,testservice){

            $scope.test = "hello "+ testvalue;

            $scope.output = testservice.lable();

        });

        </script>

        <title>learing argularjs--widuu</title>

    </head>

    <body ng-app='myapp' ng-controller='mytest' >

    <input ng-model='test'>{{test}}

    </p>

        {{output}}

    </body>

</html>

angularjs基础教程

今天就写到这里,然后以后继续.

Javascript 相关文章推荐
jQuery 插件 将this下的div轮番显示
Apr 09 Javascript
监控 url fragment变化的js代码
Apr 19 Javascript
当鼠标移动到图片上时跟随鼠标显示放大的图片效果
Jun 06 Javascript
浅析JS获取url中的参数实例代码
Jun 14 Javascript
javascript实现页面滚屏效果
Jan 17 Javascript
基于Node.js模板引擎教程-jade速学与实战1
Sep 17 Javascript
微信小程序实现tab切换效果
Nov 21 Javascript
vue使用技巧及vue项目中遇到的问题
Jun 04 Javascript
详解vue的数据劫持以及操作数组的坑
Apr 18 Javascript
详解微信小程序实现跑马灯效果(附完整代码)
Apr 29 Javascript
vue引入静态js文件的方法
Jun 20 Javascript
快速解决vue2+vue-cli3项目ie兼容的问题
Nov 17 Vue.js
jQuery中detach()方法用法实例
Dec 25 #Javascript
jQuery中remove()方法用法实例
Dec 25 #Javascript
jQuery中replaceWith()方法用法实例
Dec 25 #Javascript
jQuery中before()方法用法实例
Dec 25 #Javascript
2014 年最热门的21款JavaScript框架推荐
Dec 25 #Javascript
jQuery中after()方法用法实例
Dec 25 #Javascript
jQuery中prepend()方法用法实例
Dec 25 #Javascript
You might like
用php+mysql一个名片库程序
2006/10/09 PHP
PHP实现文件下载详解
2014/11/27 PHP
编写PHP脚本过滤用户上传的图片
2015/07/03 PHP
php使用json_decode后数字对象转换成了科学计数法的解决方法
2017/02/20 PHP
thinkPHP5.0框架独立配置与动态配置方法
2017/03/17 PHP
Laravel 修改默认日志文件名称和位置的例子
2019/10/17 PHP
jQuery 位置函数offset,innerWidth,innerHeight,outerWidth,outerHeight,scrollTop,scrollLeft
2010/03/23 Javascript
用jquery设置按钮的disabled属性的实现代码
2010/11/28 Javascript
分页栏的web标准实现
2011/11/01 Javascript
js三种排序算法分享
2012/08/16 Javascript
实测jquery data()如何存值
2013/08/18 Javascript
javascript实现日期按月份加减
2015/05/15 Javascript
jquery获取当前元素索引值用法实例
2015/06/10 Javascript
浅谈jQuery中setInterval()方法
2015/07/07 Javascript
jquery地址栏链接与a标签链接匹配之特效代码总结
2015/08/24 Javascript
angular动态删除ng-repaeat添加的dom节点的方法
2017/07/20 Javascript
手机注册发送验证码倒计时的简单实例
2017/11/15 Javascript
js点击事件的执行过程实例分析【冒泡与捕获】
2020/04/11 Javascript
Python的ORM框架SQLAlchemy入门教程
2014/04/28 Python
python正则实现提取电话功能
2018/02/24 Python
在python中将list分段并保存为array类型的方法
2019/07/15 Python
如何使用python切换hosts文件
2020/04/29 Python
Python网页解析器使用实例详解
2020/05/30 Python
python help函数实例用法
2020/12/06 Python
matplotlib运行时配置(Runtime Configuration,rc)参数rcParams解析
2021/01/05 Python
蒂芙尼澳大利亚官方网站:Tiffany&Co. Australia
2017/08/27 全球购物
华硕新加坡官方网上商店:ASUS Singapore
2020/07/09 全球购物
工业学校毕业生自荐书
2014/01/03 职场文书
考试没考好检讨书
2014/01/31 职场文书
安全目标责任书
2014/07/22 职场文书
公司委托书格式
2014/08/01 职场文书
巾帼文明岗事迹材料
2014/12/24 职场文书
酒店员工手册范本
2015/05/14 职场文书
环保守法证明
2015/06/24 职场文书
2016年校园重阳节广播稿
2015/12/18 职场文书
Python import模块的缓存问题解决方案
2021/06/02 Python