angular中ng-repeat ng-if 中的变量的值控制器中为什么取不到

这个问题的本质是:v-repeat会产生子scope,这时你在控制器里拿值,相当于父scope里面取子scope的值,因为Angular.js中作用域是向上查找的,所以取不到。

操作过程如下:

  

相关代码如下:

<table>
    <tr>
        <th>序号</th><th>姓名</th><th>工资</th><th>操作</th>
    </tr>
    <tr>
        <td>{{$index+1}}</td>
        <td>{{item.name}}</td>
        <td><input name="salary" ng-model="salary" /></td>
        <td><button ng-click="myAlert()">弹出工资</button></td>
    </tr>
</table>
<script>
    QryListCtrl.$inject = [‘$scope‘, ‘$remote‘];
    function QryListCtrl($scope, $remote){
        $scope.myAlert = function(){
            alert($scope.salary);
        }
    }
</script>

解决方法:

<table>
    <tr>
        <th>序号</th><th>姓名</th><th>工资</th><th>操作</th>
    </tr>
    <tr>
        <td>{{$index+1}}</td>
        <td>{{item.name}}</td>
        <td><input name="salary" ng-model="$parent.salary" /></td>
        <td><button ng-click="myAlert()">弹出工资</button></td>
    </tr>
</table>

原理:把子scope中的值通过 $parent属性传递给父scope即可。

时间: 2024-08-11 03:33:17

angular中ng-repeat ng-if 中的变量的值控制器中为什么取不到的相关文章

Debug查看Struts2中ExceptionMappingInterceptor拦截器怎么把ExceptionHolder放入值栈中,以及理解拦截器的工作原理。。。

1.小案例代码: jsp界面: <body> <s:property value="exceptionStack"/><br> <s:property value="exception"/><br> <s:property value="exception.getMessage"/> <form action="product-details.action&q

matlab中字符串分割以及将工作区的变量写入txt文本中

一 字符串分割 matlab中最常用的字符串分割函数有两个,都比较好用,分别是strsplit和strtok. 1 strsplit 假设需要分割的字符串为str,直接使用 strsplit(str) 就可以分割,默认按空白字符分割,分割后的字符组成元胞数组. >> str = 'hello world, I am a student!' str = hello world, I am a student! >> s = strsplit(str); >> s s =

ThinkPHP3.2中if判断条件是两个变量

<select name="typeId"> <foreach name="typeInfo" item="v"> <if condition="$v[typeId] eq $article[typeId]"> <option value="{$v.typeId}" selected>{$v.name}</option> <else/>

SQLserver2008如何把表格变量传递到存储过程中

在Microsoft SQL Server 2008中,你可以实现把表格变量传递到存储过程中,如果变量可以被声明,那么它就可以被传递.下面我们来具体介绍如何把表格变量(包括内含的数据)传递到存储过程和功能中去. 传递表值参数 用户经常会碰到许多需要把数值容器而非单个数值放到存储过程里的情况.对于大部分的编程语言而言,把容器数据结构传递到例程里或传递出来是很常见而且很必要的功能.TSQL也不例外.SQL Server 2000通过OPENXML可以实现这个功能,用户可以把数据存储为VARCHAR数

CI控制器中设置在其它方法中可用的变量

开发过程中,某些变量可能需要被控制器中的其它方法所调用,这个变量改怎么设置呢? 其实可以用ci的$this->load->vars($array);和$this->load->get_var($key);来实现. 数组可以在构造函数中设置,在方法中可以用get_var($key)获取value. class User extends CI_Controller{ public function __construct() { parent::__construct(); $arr

struts框架值栈问题七之EL表达式也会获取到值栈中的数据

7. 问题七:为什么EL也能访问值栈中的数据? * StrutsPreparedAndExecuteFilter的doFilter代码中 request = prepare.wrapRequest(request); > 对Request对象进行了包装 ,StrutsRequestWrapper > 增强了request的 getAttribute Object attribute = super.getAttribute(s); if (attribute == null) { attrib

angularjs中常用的ng指令介绍【转载】

原文:http://www.cnblogs.com/lvdabao/p/3379659.html 一.模板中可使用的东西及表达式 模板中可以使用的东西包括以下四种: 指令(directive).ng提供的或者自定义的标签和属性,用来增强HTML表现力. 标记(markup).即双大括号{{}},可将数据单向绑定到HTML中. 过滤器(filter).用来格式化输出数据. 表单控制.用来增强表单的验证功能. 其中,指令无疑是使用量最大的,ng内置了很多指令用来控制模板,如ng-repeat,ng-

SWIFT中的repeat...while

SWIFT中的repeat...while类似于JAVA\.NET中的 do while.大同小异只是把do换成了repeat var index = 10 repeat{ print(index) index-- } while(index>0)

在Angular外部使用js调用Angular控制器中提供的函数方法或变量

Html代码如下所示: 1 <!DOCTYPE html> 2 <html ng-app="myApp" id="myApp"> 3 <head> 4 <meta name="viewport" content="width=device-width" /> 5 <title>Test</title> 6 <script src="~/Co