首页 > 其他 > 详细

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

时间:2016-12-26 00:41:33      阅读:297      评论:0      收藏:0      [点我收藏+]

这个问题的本质是: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即可。

 

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

原文:http://www.cnblogs.com/wenhandi/p/6220793.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!