AngularJS之手动加载模块app和controller
2024-08-24 15:14:36
使用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将会出错
解决方法:
- 在业务代码后面给dom添加controller指令
- 使用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');
最新文章
- 125个工具与技术(PMBOK2008)
- Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) D. Santa Claus and a Palindrome STL
- POJ 2255 Tree Recovery(根据前序遍历和中序遍历,输出后序遍历)
- MySQL基础之第10章 查询数据
- 网站HTTP请求过程解析
- Codeforces Round #322 (Div. 2) B. Luxurious Houses 水题
- jquery如何选择带有多个class的元素
- WebSphere性能优化的几个方法
- laravel安装说明
- phome_ecms_news 数据表字段解释(新闻系统模型-主表)
- redis快照持久化和aof日志持久化
- Zynq PS和PL间的连接
- 转载:MySQL:亲测备份策略实例(线上真实备份案例)
- HTML、CSS知识点,面试开发都会需要--No.6 设置背景
- 基于SQL和PYTHON的数据库数据查询select语句
- 01: RestfulAPI与HTTP
- 转 jmeter使用IP欺骗压力测试
- [MongoDB]安装MongoDB遇到问题
- 使用牛顿迭代法和二分法求解一个数的平方根(python语言实现)
- MFC多语言程序版本,在不同的windows系统上的使用 FP_SetThreadUILanguage
热门文章
- BZOJ 3229: [Sdoi2008]石子合并
- 个推,手机推送api的使用
- (转) C/C++中const关键字详解
- [Prodinner项目]学习分享_第二部分_Entity到DB表的映射
- 【转】IP分片和TCP分段
- 匿名函数和Lamda
- Deep Learning 13_深度学习UFLDL教程:Independent Component Analysis_Exercise(斯坦福大学深度学习教程)
- oracle表空间不足相关问题解决办法
- Echarts的基本用法
- JavaWeb前端: JavaScript 简介