双向绑定测试:

<body ng-app>
请输入姓名:<input ng-model="myname">
<br>
{{myname}},你好
</body>

这时候AngularJS会自动绑定对象myname,并且在下放的{{myname}}中显示出来

初始化指令:

  通过ng-init来初始化对象,使对象有默认的数值:

<body ng-app   ng-init="myname='陈大海'">
  请输入你的姓名:<input ng-model="myname">
  <br>
  {{myname}},你好
</body>

此时默认显示出来的内容:

陈大海,你好

控制器:

<script >
      //定义一个对象函数
var app =angular.module('myApp',[]);
      //定义对象的controller,叫做myController,后方就是controller的内容
app.controller('myController',function($scope){
$scope.add=function(){
return parseInt($scope.x)+parseInt($scope.y)
}
});
</script>

调用

<body ng-app="myApp" ng-controller="myController">
x:<input ng-model="x" >
y:<input ng-model="y" >
运算结果:{{add()}}
</body>

也可以用$scope.x=10,设定x默认值为10

事件指令:

<head>
<title>
demo
</title>
<script src="angular.min.js"></script>
<script >
var app =angular.module('myApp',[]);
app.controller('myController',function($scope){
$scope.x=10;
$scope.add=function(){
$scope.z=parseInt($scope.x)+parseInt($scope.y); }
});
</script>
</head>
<body ng-app="myApp" ng-controller="myController">
x:<input ng-model="x" >
y:<input ng-model="y" >
<button ng-click="add()">运算</button>
运算结果:{{z}}
</body>

创建$scope.z对象和函数,但是并不主动调用函数,当按下ng-click的时候,进行add()的调用,然后主动回显结果

循环:

  

<script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script>
<script>
var app = angular.module("myApp", []);
app.controller("myController", function($scope) {
$scope.list = [10,20,30,40]
});
</script>
</head>
<body ng-app="myApp" ng-controller="myController">
<table>
<tr ng-repeat="x in list">
<td>{{x}}</td>
</tr>
</table>
</body

先用$scope创建一个list

在用ng-repeat命令调用该list

循环数组:

app.controller("myController", function($scope) {
$scope.list= [
{name:'张三',shuxue:100,yuwen:93},
{name:'李四',shuxue:88,yuwen:87},
{name:'王五',shuxue:77,yuwen:56}
]; });
</script>
</head>
<body ng-app="myApp" ng-controller="myController">
<table>
<tr>
<td>姓名</td>
<td>数学</td>
<td>语文</td>
</tr> <tr ng-repeat="entity in list">
<td>{{entity.name}}</td>
<td>{{entity.shuxue}}</td>
<td>{{entity.yuwen}}</td>
</tr> </table>

类似类的调用。

内置服务:$http

<script>
var app = angular.module("myApp",[]);
app.controller("myController",function($scope,$http){
$scope.findAll=function(){
$http.get("data.json").success(
function(response){
$scope.list=response;
}
);
}
});
</script>
</head>
<body ng-app="myApp" ng-controller="myController" ng-init="findAll()">
<table>
<tr>
<td>姓名</td>
<td>数学</td>
<td>语文</td>
</tr>
<tr ng-repeat="entity in list">
<td>{{entity.name}}</td>
<td>{{entity.shuxue}}</td>
<td>{{entity.yuwen}}</td>
</tr>
</table>

data.json

[
{"name":"张三","shuxue":100,"yuwen":93},
{"name":"李四","shuxue":88,"yuwen":87},
{"name":"王五","shuxue":77,"yuwen":56},
{"name":"赵六","shuxue":67,"yuwen":86}
]

要注意<body ng-init="findAll()"> 要初始化angularJS的函数,即$scope.findAll(),不然http会注入失败。

AngularJS入门-demo的更多相关文章

  1. AngularJS入门Demo

    1 :表达式 <html> <head> <title>入门小Demo-1</title> <script src="angular.m ...

  2. angularJS入门小Demo【简单测试js代码的方法】

    1.首先建立一个文件夹 demo, 2.在其中建立一个文本文档,改名为 demo-1.html, 3.把html中要引入的 js 文件拷贝到 demo目录下, 4.然后用 Notepadd++ 编辑刚 ...

  3. AngularJS - 入门小Demo

    AngularJS四大特效 MVC模式.模块化设计.自动化双向数据绑定.依赖注入 如果了解了后端开发知识,想必对这些词汇不会陌生,AngularJS融合了后端开发的一些思想,虽然身为前端框架,但与jQ ...

  4. AngularJS入门心得4——漫谈指令scope

    上篇<AngularJS入门心得3——HTML的左右手指令>初步介绍了指令的概念和作用.已经和指令打过一个照面,就不会那么陌生了,今天主要介绍的是一个困扰了我很久终于想通的问题,这个问题与 ...

  5. AngularJS入门心得3——HTML的左右手指令

    在<AngularJS入门心得1——directive和controller如何通信>我们提到“AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为静态文 ...

  6. AngularJS入门心得2——何为双向数据绑定

    前言:谁说Test工作比较轻松,最近在熟悉几个case,差点没疯.最近又是断断续续的看我的AngularJS,总觉得自己还是没有入门,可能是自己欠前端的东西太多了,看不了几行代码就有几个常用函数不熟悉 ...

  7. AngularJS入门心得1——directive和controller如何通信

    粗略地翻了一遍<JavaScript DOM编程艺术>,就以为可以接过AngularJS的一招半式,一个星期过去了,我发现自己还是Too Young,Too Simple!(刚打照面的时候 ...

  8. (一)Angularjs - 入门

    AngularJS进行应用开发的一个重要的思维模式: 从构造声明式界面入手 ng-app: 这个指定定义并且关联了使用angularJS的HTML页面部分 ng-model: 这个指定定义并绑定Ang ...

  9. AngularJS入门教程:日期格式化

    AngularJS入门教程:日期格式化[转载] 本地化日期格式化: ({{ today | date:'medium' }})Nov 24, 2015 2:19:24 PM ({{ today | d ...

随机推荐

  1. centos nginx配置https

    1.获取https证书: 用的阿里的免费证书: 参考:https://blog.csdn.net/chandoudeyuyi/article/details/71246255 2.修改nginx配置文 ...

  2. 常用的js代码合集

    !function(util){ window.Utils = util(); }( function(){ //document_event_attributes var DEA = "d ...

  3. 为什么移动构造要使用noexcept

    vector::push_back操作,保证了如果过程中出现异常,vector不会发生变化. push_back过程中,可能会因为已有内存空间不够,申请新的内存,同时把原内存中已有的元素,放到新申请的 ...

  4. 2.表单与PHP

    不管是一般的企业网站还是复杂的网络应用,都离不开数据的添加.通过PHP服务器端脚本语言,程序可以处理那些通过浏览器对Web应用进行数据调用或添加的请求. 回忆一下平常使用的网站数据输入功能,不管是We ...

  5. PostgreSQL在windows 10上的下载和安装

    一.下载 PostgreSQL Windows版本下载地址: https://www.postgresql.org/download/windows/ 下载地址包含2个版本,根据个人喜好下载即可: 1 ...

  6. sklearn中的SVM

    scikit-learn中SVM的算法库分为两类,一类是分类的算法库,包括SVC, NuSVC,和LinearSVC 3个类.另一类是回归算法库,包括SVR, NuSVR,和LinearSVR 3个类 ...

  7. 手眼标定eye-to-hand 示例:handeye_stationarycam_calibration

    * * This example explains how to use the hand eye calibration for the case where* the camera is stat ...

  8. bash小技巧1 获取文件当前路径

    我们linux获取文件当前路径一般问 #逼格不够高 [root@xxxx]# pwd 高逼格 SHELL_FOLDER=$(dirname $(readlink -f "$0"))

  9. 循环输入到列表的基础方法 -----python-----

    print('向列表中添加元素(输入“#”结束)\n并查看添加完的列表') list1=[] while 1: username=input('>>>') if (username. ...

  10. echarts饼图去除鼠标移入高亮

    1:如果echarts的js文件为压缩版本,在编辑器打开去掉t.on("mouseover",c).on("mouseout",d)这一行,如果js文件为非压缩 ...