AngularJS基础语法

一、ng-app

决定了angularjs的做用域范围,你能够以下使用:javascript

<html ng-app> 
…  
</html>

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


<div ng-app='myapp'>
……
</div>

来渲染其中的一部分。java

二、ng-model

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

<!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>

三、angular.module

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

angular.module(name, [requires], [configFn]);
#name       类型string建立的检索模块的名称
#requires   简单理解就是你须要包含的使用模块,譬如ngRoute路由模块
#configFn   能够选配的功能模块,功能和module.config相似

四、controller

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

<!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>

五、value

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

<!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>

六、factory

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

复制代码 代码以下:ui

<!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>

七、provider

provider也是angular.Module中的方法provider(name, providerType);其中name是service的名称,providerFunction是函数用于建立新的服务器对象,这个跟factory差很少,咱们如今用provider重写this

复制代码 代码以下:

<!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>

八、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>

九、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> 
相关文章
相关标签/搜索