angularjs笔记
  • Introduction
  • 依赖注入
  • 作用域
  • 数据绑定
    • 控制器->模板->控制器->模板
  • 多个控制器
  • $scope对象
    • $apply方法、$digest方法
    • $watch方法
    • 示例:购物车
  • 模块和控制器
  • $provide对象的provider、factory、service方法
    • 多个控制器内共享数据
  • 过滤器(Filters)
    • number、currency、date
    • limitTo、lowercase、uppercase
    • filter、orderBy、json
    • 示例:产品列表
    • 自定义过滤器
  • 控制器(controller)
    • 显式和隐式依赖注入
  • 指令
    • Angular 内置指令
    • Angular 自定义指令
      • restrict、template、replace属性
      • templateUrl属性
      • transclude、priority、terminal属性
      • compile && link属性
      • controller && controllAs属性
      • require属性
      • scope属性
  • Module里其他方法
    • constant、value、run方法
  • Form表单
    • 示例:注册页
  • XHR和服务器端的通信
    • $http
  • JavaScript基础
    • .isFunction是否为函数
  • 资料链接
  • 功能组件
    • Tooltip
    • CSV
  • 其他
    • $watchCollection
    • $controller 继承
    • 两个对象合并
    • 页面关闭
    • AngularJs 时间格式化处理
    • 数据的本地存储
    • 页面的URL
Powered by GitBook
On this page

Was this helpful?

多个控制器

  • 注意多个控制器间的作用域问题

  • ng-app指令告诉Angular应该管理页面中的哪一块

  • 注意作用域

index.html

<script src="//cdn.bootcss.com/angular.js/1.4.8/angular.js"></script>
<script type="text/javascript" src="app/index.js"></script>
<div ng-app="myApp">
    <div ng-controller="firstController">
        <input type="text" value="" ng-model="name"/>
    </div>

    <div ng-controller="secondController">
        <input type="text" value="" ng-model="name"/>
    </div>
</div>

app/index.js

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

app.controller('firstController', function($scope){
    $scope.name = '张三';
    console.log($scope);

});

app.controller('secondController', function($scope){
    console.log($scope);

});

上面的代码均在myApp块中,其中有firstController和secondController两个控制器指令,而且是平级,所以secondController中的name不会受到影响。

效果

当firstController包含secondController层级时,将会出现作用域的问题,secondController其实并未有name变量的赋值,这是则会向父级中找。

    <div ng-app="myApp">

        <div ng-controller="firstController">
            <input type="text" value="" ng-model="name"/>
            <div ng-controller="secondController">
            <input type="text" value="" ng-model="name"/>
        </div>
        </div>        
    </div>
    <script src="//cdn.bootcss.com/angular.js/1.4.8/angular.js"></script>
    <script type="text/javascript" src="app/index.js"></script>

效果

Previous控制器->模板->控制器->模板Next$scope对象

Last updated 5 years ago

Was this helpful?