使用ng的页面中一般都是使用模块自动加载,页面的结构一般是这样的

  • 加载angularjs脚本
  • 加载业务代码脚本(或者写在script标签中)
  • html结构代码(带有ng指令)

就像这样

app.html

<html>
<head>
<script src="angular.js"></script>
<script src="mypage.js"></script>
</head>
<body ng-app="app" ng-controller="ctrl">
<h1 ng-bind="Model.Name"></h1>
</body>
</html>
mypage.js

var app = angular.module("app", []);
var ctrl = app.controller("ctrl", function ($scope, $http) {
$scope.Model = {
Name: "ABC"
};
});

大部分情况mypage.js只要在angularjs后面的任意位置都可以。

但是,如果mypage.js是异步加载的,例如script加上了 async,或者使用requirejs等模块化脚本,那么ng将会出错

解决方法:

  1. 在业务代码后面给dom添加controller指令
  2. 使用angularjs的模块手动加载函数bootstrap

要注意的是,module和controller(即下面的app和ctrl)的定义 要在bootstrap执行之前

var app = angular.module("app", []);
var ctrl = app.controller("ctrl", function ($scope, $http) {
$scope.UI = {
Model: {
NickName: "ABC",
Password: ""
}
};
});

angular.bootstrap(document, ['app']);
angular.element(document).find('body').attr({ "ng-controller": "Ctrl" });
angular.element(document).find('html').addClass('ng-app');

最新文章

  1. 125个工具与技术(PMBOK2008)
  2. Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) D. Santa Claus and a Palindrome STL
  3. POJ 2255 Tree Recovery(根据前序遍历和中序遍历,输出后序遍历)
  4. MySQL基础之第10章 查询数据
  5. 网站HTTP请求过程解析
  6. Codeforces Round #322 (Div. 2) B. Luxurious Houses 水题
  7. jquery如何选择带有多个class的元素
  8. WebSphere性能优化的几个方法
  9. laravel安装说明
  10. phome_ecms_news 数据表字段解释(新闻系统模型-主表)
  11. redis快照持久化和aof日志持久化
  12. Zynq PS和PL间的连接
  13. 转载:MySQL:亲测备份策略实例(线上真实备份案例)
  14. HTML、CSS知识点,面试开发都会需要--No.6 设置背景
  15. 基于SQL和PYTHON的数据库数据查询select语句
  16. 01: RestfulAPI与HTTP
  17. 转 jmeter使用IP欺骗压力测试
  18. [MongoDB]安装MongoDB遇到问题
  19. 使用牛顿迭代法和二分法求解一个数的平方根(python语言实现)
  20. MFC多语言程序版本,在不同的windows系统上的使用 FP_SetThreadUILanguage

热门文章

  1. BZOJ 3229: [Sdoi2008]石子合并
  2. 个推,手机推送api的使用
  3. (转) C/C++中const关键字详解
  4. [Prodinner项目]学习分享_第二部分_Entity到DB表的映射
  5. 【转】IP分片和TCP分段
  6. 匿名函数和Lamda
  7. Deep Learning 13_深度学习UFLDL教程:Independent Component Analysis_Exercise(斯坦福大学深度学习教程)
  8. oracle表空间不足相关问题解决办法
  9. Echarts的基本用法
  10. JavaWeb前端: JavaScript 简介