angular.js

AngularJS 表达式

AngularJS 表达式写在双大括号内:{{ expression }}
AngularJS 表达式把数据绑定到 HTML,这与 ng-bind 指令有异曲同工之妙。
AngularJS 将在表达式书写的位置"输出"数据。
AngularJS 表达式 很像 JavaScript 表达式:它们可以包含文字、运算符和变量。

  • 数字
<div ng-app="">  
     <p>我的第一个表达式: {{ 5 + 5 }}</p>
</div>
<div ng-app="" ng-init="quantity=1;cost=5">
      <p>总价: {{ quantity * cost }}</p>
</div>
<div ng-app="" ng-init="quantity=1;cost=5">
      <p>总价: <span ng-bind="quantity * cost"></span></p>
</div>
  • 字符串
<div ng-app="" ng-init="firstName='John';lastName='Doe'">
       <p>姓名: {{ firstName + " " + lastName }}</p>
</div>
<div ng-app="" ng-init="firstName='John';lastName='Doe'">
        <p>姓名: <span ng-bind="firstName + ' ' + lastName"></span></p>
</div>
  • 对象
<div ng-app="" ng-init="person={firstName:'John',lastName:'Doe'}">
       <p>姓为 {{ person.lastName }}</p>
</div>
<div ng-app="" ng-init="person={firstName:'John',lastName:'Doe'}">
       <p>姓为 <span ng-bind="person.lastName"></span></p>
</div>
  • 数组
<div ng-app="" ng-init="points=[1,15,19,2,40]">
       <p>第三个值为 {{ points[2] }}</p>
</div>
<div ng-app="" ng-init="points=[1,15,19,2,40]">
      <p>第三个值为 <span ng-bind="points[2]"></span></p>
</div>

AngularJS 指令

AngularJS 指令是扩展的 HTML 属性,带有前缀 ng-
ng-app 指令初始化一个 AngularJS 应用程序。
ng-init 指令初始化应用程序数据。
ng-model 指令把元素值(比如输入域的值)绑定到应用程序。

<div ng-app="" ng-init="firstName='John'">  
      <p>在输入框中尝试输入:</p>     
      <p>姓名:<input type="text" ng-model="firstName"></p>    
      <p>你输入的为: {{ firstName }}</p>
</div>
  • 数据绑定
<div ng-app="" ng-init="quantity=1;price=5">
      <h2>价格计算器</h2>
      数量: <input type="number"  ng-model="quantity">
      价格: <input type="number" ng-model="price">
      <p><b>总价:</b> {{ quantity * price }}</p>
</div>
  • 重复 HTML 元素
<div ng-app="" ng-init="names=['Jani','Hege','Kai']">  
      <p>使用 ng-repeat 来循环数组</p>  
      <ul>    
            <li ng-repeat="x in names">      {{ x }}    </li> 
       </ul>
</div>
<div ng-app="" ng-init="names=[{name:'Jani',country:'Norway'},{name:'Hege',country:'Sweden'},{name:'Kai',country:'Denmark'}]">
      <p>循环对象:</p>
      <ul>  
            <li ng-repeat="x    in names">    {{ x.name + ', ' + x.country }}  </li>
      </ul>
</div>
  • 创建自定义的指令

AngularJS 通过被称为 指令 的新属性来扩展 HTML。
AngularJS 通过内置的指令来为应用添加功能。
AngularJS 允许你自定义指令。

<body ng-app="myApp">
      <runoob-directive></runoob-directive>
      <script>
            var app = angular.module("myApp", []);
            app.directive("runoobDirective", function() {   
                return {       
                    template: "<h1>自定义指令!</h1>"   
                };
            });
      </script>
</body>
<script type="text/javascript">
            var app = angular.module("myApp", []);
            app.directive("runoobDirective", function() {
                return {
                    restrict: "A",
                    template: "<h1>自定义指令!</h1>"
                };
            });
 </script>

restrict 值可以是以下几种:
E 作为元素名使用
A 作为属性使用
C 作为类名使用
M 作为注释使用
restrict 默认值为 EA, 即可以通过元素名和属性名来调用指令。

df
d
fd
fd
f
df
d
f
d
f
df
d
f
dfd
f
d
f
df
d
f








d
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容