AngularJS已经被很多人像炒冷饭一样炒过啦,大部分都是直接复制官方文档没有说明一些注意事项,不过什么都要从头开始吧

页面引导实例化


1.自动实例化

<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular.js"></script>
<body ng-app="app">1+2={{1+2}}</body>
<script>angular.module('app',[])</script>
</html>

如果不写app这个值那么script的内容可以不写

2.手动实例化(主要通过js来引导)

<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular.js"></script>
<body>1+2={{1+2}}</body>
<script>
    angular.module('app',[])
      .conifg(function(){})
      .run(function(){alert('start');});
    angular.bootstrap(document, ['app']);
</script>
</html>
  • 为什么声明一个模块后面有一个中括号?
  • 因为angular是模块开发,声明一个模块必须要依赖另一个模块或者空模块,否则就声明失效啦
  • config的方法可以配置模块,run是在模块执行前所执行的方法

模块的依赖调用


angular.module('route',[]);
angular.module('app',['route']);

就是这么简单直接route模块的东西都可以被app模块所调用声明一个模块必须要依赖另一个模块或者空模块

模块的控制器($scope这个参数即为控制器的上下文)


1.调用模块的controller方法即可声明一个控制器而且返回当前模块所以可以用级联,

<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular.js"></script>
<body ng-controller="a">{{data}}</body>
<script>
  angular.module('app.controller',[])
    .controller('a',['$scope',function($scope){
        $scope.data=1;
    }]);
  angular.module('app',['app.controller']);
  angular.bootstrap(document, ['app']);
</script>
</html>
  • 为什么声明一个$scope这个变量?
  • 因为当进行压缩js代码的时候变量名称改变,为了第一个参数为$scope这个变量而设计的,
  • 所以可以写成
angular.module('app.controller',[])
  .controller('a',['$scope',function(c){  
    c.data=1;
  }]);
  • 如果不压缩也可以写成
angular.module('app.controller',[])
  .controller('a',function($scope){  
    $scope.data=1;
  });

2.直接声明一个全局方法

function a(a){c.data=1;}
a.$inject = ['$scope'];
angular.module('app',[]);
angular.bootstrap(document, ['app']);

controller$inject对象也是同上原因声明第一个参数为$scope,如果不压缩可以忽略

数据绑定


数据绑定是用了2个大括号,并且获取controller中的数据,如果使用指令则{{}}可以省略直接写属性


Ⅰ.AngularJS的点点滴滴--引导的更多相关文章

  1. Ⅱ.AngularJS的点点滴滴--缓存

    模板缓存-$templateCache and 缓存工厂 $cacheFactory 1.使用script标签 <html ng-app> <script src="htt ...

  2. Ⅶ.AngularJS的点点滴滴-- 事件

    事件(和js一样有冒泡和捕获) <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2 ...

  3. Ⅵ.AngularJS的点点滴滴-- 指令

    指令 基本用法 <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angul ...

  4. Ⅴ.AngularJS的点点滴滴-- 资源和过滤

    资源ngResource(依赖ngResource模块) <html> <script src="http://ajax.googleapis.com/ajax/libs/ ...

  5. Ⅳ.AngularJS的点点滴滴-- 服务

    服务(Angularjs很多方法都是服务组成的) 1.使用service方法创建的单例服务 <html> <script src="http://ajax.googleap ...

  6. Ⅲ.AngularJS的点点滴滴-- 路由

    路由ngRoute (需要依赖ngRoute模块) <html> <script src="http://ajax.googleapis.com/ajax/libs/ang ...

  7. angularjs 笔记(1) -- 引导

    首先: 1,引入angularJS文件,<script type="text/javascript" src="angularjs.min.js"> ...

  8. AngularJS的启动引导过程

    原文:http://www.angularjs.cn/A137?utm_source=ourjs.com 目录: 引导之前 自动引导启动框架 手工引导启动框架 引导第1步:创建注入器 引导第2步:创建 ...

  9. AngularJS1

    Ⅰ.AngularJS的点点滴滴--引导   AngularJS已经被很多人像炒冷饭一样炒过啦,大部分都是直接复制官方文档没有说明一些注意事项,不过什么都要从头开始吧 ###页面引导实例化 * * * ...

随机推荐

  1. [topcoder]TopographicalImage

    BFS.这里用了queue,以及在数据结构里存了上一个的高度.也可以递归调用完成BFS,在调用之前做判断:http://community.topcoder.com/stat?c=problem_so ...

  2. 安装Chive提示CDbConnection failed to open the DB connection.

    最近初学PHP,看到Chive这个好玩的数据库管理工具,在登录时遇到这样的错误提示信息: CDbConnection failed to open the DB connection. 我的PHP版本 ...

  3. 搭建Eclipse C/C++开发环境

    搭建eclipse C/C++开发环境:     1.下载并安装Eclipse for C++:http://www.eclipse.org.最新版是基于Eclipse 3.5 galileo,文件名 ...

  4. When to use DataContract and DataMember attributes?

    When to use DataContract and DataMember attributes? I  am very confused about the DataContract attri ...

  5. Uncompressing Linux___ done, booting the kernel_tekkamanninja-ChinaUnix博客

    今天用主线Linux内核移植到MINI6410,主线内核2.6.37.1基本已经支持了MINI6410的板子,所以移植到能够启动起来的阶段很简单,但是在移植的时候还是出现了一个比较常见的问题: MIN ...

  6. bzoj1758

    好题显然是分数规划,二分答案之后我们要找是否存在一条边数在[l,u]长度和为正的路径可以用树的分治来解决这个问题我们假设当前处理的是过点root的路径显然我们不好像之前男人八题里先算出所有答案,然后再 ...

  7. EXP-00056: ORACLE error 6550 encountered报错;

    SQL> exp sys/sys file=/home/oracle/sys.dmp full=y Export: Release 11.2.0.3.0 - Production on Wed ...

  8. Ruby 程序员最要好的朋友

    Ruby 是... 一种跨平台.面向对象的动态类型编程语言.Ruby 体现了表达的一致性和简单性, 它不仅是一门编程语言,更是表达想法的一种简练方式. https://www.ruby-lang.or ...

  9. Windows和Ubuntu双系统,修复UEFI引导的两种办法

    查看ubuntu是否是从 efi/uefi 启动的方法:  若 /sys/firmware/efi 存在则是,否则不是.shell命令: [ -d /sys/firmware/efi ] && ...

  10. 机器学习笔记1——Linear Regression with One Variable

    Linear Regression with One Variable Model Representation Recall that in *regression problems*, we ar ...