angular js 指令 ng-model与 no-repeat的使用 ng-各种指令 创建自定义指令 限制使用指令 restrict的不同取值

AngularJS 指令是扩展的 HTML 属性,带有前缀 ng-

ng-app 指令初始化一个 AngularJS 应用程序。

ng-init 指令初始化应用程序数据。

ng-model 指令把元素值(比如输入域的值)绑定到应用程序。

实例   ({{ firstName }} 是通过 ng-model="firstName" 进行同步。)

<div ng-app="" ng-init="firstName='John'">

<p>在输入框中尝试输入:</p>

<p>姓名:<input type="text" ng-model="firstName"></p>

<p>你输入的为: {{ firstName }}</p>

</div>

 利用ng-model同步功能制作类似于淘宝购物车效果

   <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>

  

使用 ng-repeat 来循环数组
<div data-ng-app="" data-ng-init="names=['Jani','Hege','Kai']">
  <p>使用 ng-repeat 来循环数组</p>
  <ul>
    <li data-ng-repeat="x in names">
      {{ x }}
    </li>
  </ul>
</div>

ng-repeat 指令用在一个对象数组上: 

<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>
 

ng-app 指令定义了 AngularJS 应用程序的 根元素

ng-app 指令在网页加载完毕时会自动引导(自动初始化)应用程序。

ng-init 指令为 AngularJS 应用程序定义了 初始值

通常情况下,不使用 ng-init。您将使用一个控制器或模块来代替它。

ng-model 指令

ng-model 指令 绑定 HTML 元素 到应用程序数据。

ng-model 指令也可以:

  • 为应用程序数据提供类型验证(number、email、required)。
  • 为应用程序数据提供状态(invalid、dirty、touched、error)。
  • 为 HTML 元素提供 CSS 类。
  • 绑定 HTML 元素到 HTML 表单。 

    ng-repeat 指令对于集合中(数组中)的每个项会 克隆一次 HTML 元素

创建自定义的指令

除了 AngularJS 内置的指令外,我们还可以创建自定义指令。

你可以使用 .directive 函数来添加自定义的指令。

要调用自定义指令,HTML 元素上需要添加自定义指令名。

使用驼峰法来命名一个指令, runoobDirective, 但在使用它时需要以 - 分割, runoob-directive:

实例

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

你可以通过以下方式来调用指令:

  • 元素名
  • 属性
  • 类名
  • 注释
通过元素名调用上面自定义指令时直接调用不需要改变什么
元素名调用:<runoob-directive></runoob-directive>;
属性调用:<div runoob-directive></div>;


类名调用:
<div class="runoob-directive"></div>

注意: 你必须设置 restrict 的值为 "C" 才能通过类名来调用指令。
实例:
<body ng-app="myApp">

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

注释调用:directive: runoob-directive;

  注意: 我们需要在该实例添加 replace 属性, 否则评论是不可见的。

   注意: 你必须设置 restrict 的值为 "M" 才能通过注释来调用指令。

   实例:

    <body ng-app="myApp">

  <!-- directive: runoob-directive -->

  <script>
  var app = angular.module("myApp", []);
  app.directive("runoobDirective", function() {
  return {
  restrict : "M",
  replace : true,
  template : "<h1>自定义指令!</h1>"
  };
  });
  </script>

限制使用

你可以限制你的指令只能通过特定的方式来调用。

实例

通过添加 restrict 属性,并设置只值为 "A", 来设置指令只能通过属性的方式来调用:

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


restrict 值可以是以下几种:

  • E 作为元素名使用
  • A 作为属性使用
  • C 作为类名使用
  • M 作为注释使用

restrict 默认值为 EA, 即可以通过元素名和属性名来调用指令。




原文地址:https://www.cnblogs.com/yaomengli/p/6811430.html