• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com专业计算机教程网站
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure
您的位置:首页 > 网页设计 >AngularJS > AngularJS语法详解

AngularJS语法详解

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-30

本文主要包含AngularJS语法详解等相关知识,希望在学习及工作中可以帮助到您

模板和数据的基本运作流程如下:

用户请求应用起始页面
用户的浏览器向服务器发起一次http连接,然后加载index.html页面,这个页面包含了模板
angular被加载到页面中,等待页面加载完成,查找ng-app指令,用来定义模板的边界
angular遍历模板,查找指定和绑定关系,将触发一些列动作:注册监听器、执行一些DOM操作、从服务器获取初始化数据。最后,应用将会启动起来,并将模板转换成DOM视图
连接到服务器去加载需要展示给用户的其他数据

显示文本

一种使用{{}}形式,如{{greeting}} 第二种ng-bind="greeting"

使用第一种,未被渲染的页面可能会被用户看到,index页面建议使用第二种,其余的页面可以使用第一种

表单输入

<html ng-app>
<head>
    <title>表单</title>
    <script type="text/javascript" src="angular.min.js"></script>
    <script type="text/javascript">
    function StartUpController($scope) {
        $scope.funding = {startingEstimate:0};
        computeNeeded = function() {
            $scope.funding.needed = $scope.funding.startingEstimate * 10;
        };
        $scope.$watch('funding.startingEstimate',computeNeeded); //watch model的变化
    }
    </script>
</head>
<body>
    <form ng-controller="StartUpController">
        Starting: <input ng-change="computeNeeded()" ng-model="funding.startingEstimate">  //change的时候调用函数
        Recommendation: {{funding.needed}}
    </form>
</body>
</html>
</div>

在某些情况下,我们不想一有变化就立刻做出动作,而是要进行等待。例如:

<html ng-app>
<head>
    <title>表单</title>
    <script type="text/javascript" src="angular.min.js"></script>
    <script type="text/javascript">
    function StartUpController($scope) {
        $scope.funding = {startingEstimate:0};
        computeNeeded = function() {
            $scope.funding.needed = $scope.funding.startingEstimate * 10;
        };
        $scope.$watch('funding.startingEstimate',computeNeeded);//watch监视一个表达式,当这个表达式发生变化时就会调用一个回调函数
        $scope.requestFunding = function() {
            window.alert("Sorry,please get more customers first.")
        };
    }
    </script>
</head>
<body>
    <form ng-submit="requestFunding()" ng-controller="StartUpController">  //ng-submit
        Starting: <input ng-change="computeNeeded()" ng-model="funding.startingEstimate">
        Recommendation: {{funding.needed}}
        <button>Fund my startup!</button>
    </form>
</body>
</html>
</div>

非表单提交型的交互,以click为例

<html ng-app>
<head>
    <title>表单</title>
    <script type="text/javascript" src="angular.min.js"></script>
    <script type="text/javascript">
    function StartUpController($scope) {
        $scope.funding = {startingEstimate:0};
        computeNeeded = function() {
            $scope.funding.needed = $scope.funding.startingEstimate * 10;
        };
        $scope.$watch('funding.startingEstimate',computeNeeded);
        $scope.requestFunding = function() {
            window.alert("Sorry,please get more customers first.")
        };
        $scope.reset = function() {
            $scope.funding.startingEstimate = 0;
        };
    }
    </script>
</head>
<body>
    <form ng-controller="StartUpController">
        Starting: <input ng-change="computeNeeded()" ng-model="funding.startingEstimate">
        Recommendation: {{funding.needed}}
        <button ng-click="requestFunding()">Fund my startup!</button>
        <button ng-click="reset()">Reset</button>
    </form>
</body>
</html>
</div>

列表、表格以及其他迭代型元素

ng-repeat会通过$index返回当前引用的元素序号。 示例代码如下:

<html ng-app>
<head>
    <title>表单</title>
    <script type="text/javascript" src="angular.min.js"></script>
    <script type="text/javascript">
    var students = [{name:'Mary',score:10},{name:'Jerry',score:20},{name:'Jack',score:30}]
    function StudentListController($scope) {
        $scope.students = students;

    }
    </script>
</head>
<body>
    <table ng-controller="StudentListController">
        <tr ng-repeat='student in students'>
            <td>{{$index+1}}</td>
           

您可能想查找下面的文章:

相关文章

  • 2017-05-30Angularjs全局变量被作用域监听的正确姿势
  • 2017-05-30AngularJS模块学习之Anchor Scroll
  • 2017-05-30AngularJS中监视Scope变量以及外部调用Scope方法
  • 2017-05-30AngularJs expression详解及简单示例
  • 2017-05-30详解Angualr 组件间通信
  • 2017-05-30AngularJS中$http服务常用的应用及参数
  • 2017-05-30AngularJs bootstrap搭载前台框架——js控制部分
  • 2017-05-30angular.element方法汇总
  • 2017-08-14angular基本概念
  • 2017-05-30详解AngularJS Filter(过滤器)用法

文章分类

  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure

最近更新的内容

    • AngualrJS中的Directive制作一个菜单
    • 详谈angularjs中路由页面强制更新的问题
    • angular forEach方法遍历源码解读
    • AngularJs bootstrap搭载前台框架——js控制部分
    • Angular2 NgModule 模块详解
    • AngularJS报错$apply already in progress的解决方法分析
    • 浅析AngularJs HTTP响应拦截器
    • Angular2平滑升级到Angular4的步骤详解
    • ionic+AngularJs实现获取验证码倒计时按钮
    • AngularJs expression详解及简单示例

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有