GVKun编程网logo

$index的ngRepeat里面angularjs指令(angularjs指令的作用)

13

本文将为您提供关于$index的ngRepeat里面angularjs指令的详细介绍,我们还将为您解释angularjs指令的作用的相关知识,同时,我们还将为您提供关于AngularJsng-repe

本文将为您提供关于$index的ngRepeat里面angularjs指令的详细介绍,我们还将为您解释angularjs指令的作用的相关知识,同时,我们还将为您提供关于AngularJs ng-repeat with index、AngularJs ng-repeat 嵌套如何获取外层$index、AngularJs ng-repeat中的$index、AngularJS ng-repeat指令及Ajax的应用实例分析的实用信息。

本文目录一览:

$index的ngRepeat里面angularjs指令(angularjs指令的作用)

$index的ngRepeat里面angularjs指令(angularjs指令的作用)

我试图找到最好的方法来获得一个ngRepeat使用自定义指令的索引位置。我想解决的问题是,对于ngRepeat的每次迭代,我想知道我在迭代中的位置,所以我可以创建一个基于索引位置的自定义模板的选项。

除非有更好的方法这样做,我试图做这个功能基于这个文档从指令:

& or &attr – provides a way to execute an expression in the context of the parent scope. If no attr name is specified then the attribute name is assumed to be the same as the local name. Given and widget deFinition of scope: { localFn:’&myAttr’ },then isolate scope property localFn will point to a function wrapper for the count = count + value expression. Often it’s desirable to pass data from the isolated scope via an expression and to the parent scope,this can be done by passing a map of local variable names and values into the expression wrapper fn. For example,if the expression is increment(amount) then we can specify the amount value by calling the localFn as localFn({amount: 22}).

http://docs.angularjs.org/guide/directive

我很难理解这句话告诉我什么。所以,我试图做的是这…

首先,ngRepeat指令与我的指令一起使用在“testTemplate”中:

<ul>
   <li my-attr="test()" my-directive ng-repeat="value in values">
   </li>
</ul>

接下来,我的指令定义:

angular.module('myModule',[])
       .directive('myDirective',function() {
            return {
                replace : true,transclude : true,scope : {
                    test: '&myAttr'
                },templateUrl: 'partials/myTemplate.html',link : function(scope,element,attrs) {
                    alert(scope.count);
                }
            }
        });

现在终于,我试图定义的控制器内的“测试”函数为引用指令的父模板。

function TestTemplateCtrl($scope,testTemplate) {
    $scope.test = function() {
        alert('in test');
        $scope.count = "1";
    }
}

所以第一个问题是我的“测试”函数在父中没有被执行。也许我不明白如何应该调用父控制器中的测试函数。现在我实际上还没有增加,但是是上面的正确的方式,我会去增加我的计数,当我到达那一点,如果我可以得到测试功能开火?

而不是scope。$ parent。$ index你可以考虑传递$ index作为指令属性:
<li my-directive index="{{$index}}" ng-repeat="value in values">

指示:

myApp.directive('myDirective',function() {
    return {
        replace: true,// transclude: true,scope: {
            index: '@'
        },template: '<div>testing {{index}}</div>',link: function(scope,attrs) {
            // ...
        }
    }
});

Fiddle。

AngularJs ng-repeat with index

AngularJs ng-repeat with index

我只想在单击显示按钮时在此部分显示名称.我正试图检测使用索引,但我没有成功.

码:

<div ng-repeat="c in customers">

   <a ng-click="display(c.id[$index])" ng-hide="need to hide after click this one">{{vm.updateText}}</a>
   <div ng-show="c.id[$index]">
        <input type="text" name="id" data-ng-model="id" />
          <input type="button" ng-click="vm.veryId(id)" value="{{vm.verifyButtonText}}">
          <input type="button" ng-click="vm.Cancel()" value="{{vm.cancelButtonText}}">
       </div>
    </div>
    // will show ng-click="vm.veryId(id)"
    <rpe-progress data-ng-show="vm.showProgress" block="true"></rpe-progress>
    // if id is working will show following error message:
    <rpe-alert show="vm.mpnIDalert">{{vm.errormessage}}</rpe-alert>        



<script>
   vm.display= function (index) {    
      vm.id[index] = true;  
  //   I want show updatesection & hide Update text   
   }
vm.cancel= function () {   
//   I want hide updatesection & show Update text 
       }


  vm.veryId= function (id) {   
    //  If id is wrong show error message.
           }
</script>

解决方法

发布@Pevara的逻辑非常好.我建议你顺应那个逻辑.

我不明白你的问题的实际用例.所以,如果你真的想用$index值来完成这个逻辑,你可以使用下面的代码片段来完成你的任务.

var app = angular.module('app',[]);

app.controller('indexCtrl',function ($scope) {
  $scope.customers = [
    {name: 'ABC',Age: 26},{name: 'DEF',Age: 32},{name: 'GHI',Age: 21}    
  ]; 
  
  $scope.toggledisplay = function(index) {
    $scope.customers[index].show = ! $scope.customers[index].show;    
  };
  
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="app">
  <div ng-controller="indexCtrl">
    <table>
      <tr>
        <td>Name</td>
        <td>Age</td>
      </tr>
    <tr ng-repeat="c in customers">
      <td>{{c.name}}</td>
      <td>
        <button ng-click="toggledisplay($index)">display</button>            
        <span ng-show="c.show">{{c.Age}}</span></td>
    </tr>
    </table>
  </div>
</body>

AngularJs ng-repeat 嵌套如何获取外层$index

AngularJs ng-repeat 嵌套如何获取外层$index

一个真实项目的例子是遍历表格的行和列,每一行需要显示当前是第几行,我立刻想到用$index,简直就如同砍瓜切菜般,一切都那么行云流水,简直太容易了,于是有了下面这段代码.

rush:js;">

可当我的程序跑起来了,我发现我获取的$index感觉怪怪的,我想一定是我打开的方式不对,我狂按几下F5,可事实就是我的程序出BUG了,因为它拿到的是列循环的索引. 这下我懵逼了. 你TM是在逗我?

经过思索,我想到了ngInit,于是有了下面这段代码, 我尝试做的事情是把$index赋值给了outerIndex,并在循环体中输出outerIndex.

rush:js;">

这时成功得到了想要的结果. 虽然这个小问题很简单,但对于angular新手来说还是挺蛋疼的,感觉答案呼之欲出,却又欲言又止, 小小记录一下,我的angularJs之路才刚刚开始.

以上就是对AngularJs ng-repeat 嵌套如何获取外层$index 的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!

AngularJs ng-repeat中的$index

AngularJs ng-repeat中的$index

当我们使用AngularJs的ng-repeat时候动态绑定数据时,遇到遍历出来的标签样式都一样,这时候希望根据数组的下标分别对应不同的样式

<div ng-repeat="item in menulist">  
        <div>  
            <span></span> <span> {{item.DirName}} </span>  
        </div>  
    </div> 

把样式库写好就行 ok搞定

AngularJS ng-repeat指令及Ajax的应用实例分析

AngularJS ng-repeat指令及Ajax的应用实例分析

本文实例讲述了AngularJS ng-repeat指令及Ajax的应用。分享给大家供大家参考,具体如下:

ng-repeat 指令用于循环输出指定次数的 HTML 元素。集合必须是数组或对象。

定义:

<element ng-repeat="expression"></element>

说明:experssion表达式定义了如何循环集合。常用的如:x in records

下面通过一个例子,来说明ng-repeat如何绘制一个表格:

<div ng-app=''mainApp'' ng-controller=''studentController''>
  <table border="0">
    <tr>
      <td>姓</td>
      <td><input type="text" ng-model=''student.firstName''></td>
    </tr>
    <tr>
      <td>名</td>
      <td><input type="text" ng-model=''student.lastName''></td>
    </tr>
    <tr>
      <td>名字</td>
      <td>{{student.fullName()}}</td>
    </tr>
    <tr>
      <td>科目</td>
      <td>
        <table>
          <tr>
            <th>名字</th>
            <th>标记</th>
          </tr>
          <tr ng-repeat=''sub in student.subjects''>
            <td>{{sub.name}}</td>
            <td>{{sub.marks}}</td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
</div>

表可以使用CSS样式设置样式。

<style>
  table, th , td {
    border: 1px solid grey;
    border-collapse: collapse;
    padding: 5px;
  }
  table tr:nth-child(odd) {
    background-color: #f2f2f2;
  }
  table tr:nth-child(even) {
    background-color: #ffffff;
  }
</style>

利用angularjs的ng-repeat指令绘制表格:

<script>
  var mainApp=angular.module(''mainApp'',[]); //定义一个名为mainApp的模块
  mainApp.controller(''studentController'',function($scope){
    $scope.student={
      firstName:''聂'',
      lastName:''鹏飞'',
      subjects:[
      {name:''物理'',marks:73},
          {name:''化学'',marks:90},
          {name:''数学'',marks:68},
          {name:''英文'',marks:85},
          {name:''生物'',marks:77},
      ],
      fullName:function(){
        var studentObject;
        studentObject = $scope.student;
        return studentObject.firstName+'' ''+studentObject.lastName;
      }
    };
  });
</script>

效果:

上面例子表格中展示的数据也可以通过ajax请求从服务器中获取,然后利用ng-repeat指令插入到页面中,具体实现见下面代码:

<html>
<head>
<title>Angular JS Controller</title>
<script src="angularjs/angular-1.3.0/angular.min.js"></script>
<style>
  table, th , td {
    border: 1px solid grey;
    border-collapse: collapse;
    padding: 5px;
  }
  table tr:nth-child(odd) {
    background-color: #f2f2f2;
  }
  table tr:nth-child(even) {
    background-color: #ffffff;
  }
</style>
</head>
<body>
  <h2>AngularJS 表格应用示例</h2>
  <div ng-app=''mainApp'' ng-controller=''studentController''>
    <table border="0">
      <tr>
        <td>姓</td>
        <td><input type="text" ng-model=''student.firstName''></td>
      </tr>
      <tr>
        <td>名</td>
        <td><input type="text" ng-model=''student.lastName''></td>
      </tr>
      <tr>
        <td>名字</td>
        <td>{{student.fullName()}}</td>
      </tr>
      <tr>
        <td>科目</td>
        <td>
          <table>
            <tr>
              <th>名字</th>
              <th>标记</th>
            </tr>
            <tr ng-repeat=''sub in student.subjects''>
              <td>{{sub.name}}</td>
              <td>{{sub.marks}}</td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  </div>
  <script>
    var mainApp=angular.module(''mainApp'',[]);
    mainApp.controller(''studentController'',function($scope,$http){
      $scope.student={
        firstName:''聂'',
        lastName:''鹏飞'',
        fullName:function(){
          var studentObject;
          studentObject = $scope.student;
          return studentObject.firstName+'' ''+studentObject.lastName;
        },
      };
      var url="data.txt";
      $http.post(url).success(function(response){
        $scope.student.subjects=response;
      })
    });
  </script>
</body>
</html>

说明:需要放在服务器环境中运行

更多关于AngularJS相关内容感兴趣的读者可查看本站专题:《AngularJS指令操作技巧总结》、《AngularJS入门与进阶教程》及《AngularJS MVC架构总结》

希望本文所述对大家AngularJS程序设计有所帮助。

您可能感兴趣的文章:
  • AngularJs ng-repeat 嵌套如何获取外层$index
  • AngularJS入门(用ng-repeat指令实现循环输出
  • AngularJS ng-repeat数组有重复值的解决方法
  • Angularjs的ng-repeat中去除重复数据的方法
  • AngularJS使用ng-repeat指令实现下拉框
  • AngularJS基础 ng-repeat 指令简单示例
  • Angularjs中ng-repeat-start与ng-repeat-end的用法实例介绍
  • AngularJS实现ajax请求的方法
  • AngularJS入门教程之与服务器(Ajax)交互操作示例【附完整demo源码下载】
  • 实例详解angularjs和ajax的结合使用
  • 在AngularJS中使用AJAX的方法

今天的关于$index的ngRepeat里面angularjs指令angularjs指令的作用的分享已经结束,谢谢您的关注,如果想了解更多关于AngularJs ng-repeat with index、AngularJs ng-repeat 嵌套如何获取外层$index、AngularJs ng-repeat中的$index、AngularJS ng-repeat指令及Ajax的应用实例分析的相关知识,请在本站进行查询。

本文标签:

上一篇angularjs – 如何调试angular [$injector:modulerr]错误(angularjs stateprovider)

下一篇如何使用不会污染全局范围的TypeScript类定义AngularJS服务?(angularjs typescript)