为什么不输入修改NG-重复(和NG-开关)之外的价值? [英] Why doesn't an input modify a value outside of an ng-repeat (and ng-switch)?

查看:119
本文介绍了为什么不输入修改NG-重复(和NG-开关)之外的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我假定一个范围的问题是在这个例子中,输入它旁边可以修改值的原因,而不是其他的值。如果是这样的话,我怎么挂钩的模式,正确的范围是什么?如果它不是一个范围的问题,我是什么做错了吗?

 < HTML NG-应用>
    < HEAD>
        <脚本类型=文/ JavaScript的SRC =角1.0.1.min.js>< / SCRIPT>
        <脚本类型=文/ JavaScript的>
            功能ExampleCtrl($范围){
                $ scope.list = [
                    {名:A},
                    {名:B},
                ];                $ scope.value = 5;
            }
        < / SCRIPT>
    < /头>
    <机身NG控制器=ExampleCtrl>
        < UL NG重复=项列表>
            <立GT; {{item.name}}
                <有关=item.name&GT NG-开关;
                    <跨度NG-开关时=B>
                        <输入类型=数字NG-模式=值/>
                        {{值}}
                    < / SPAN>
                < / NG开关>
            < /李>
        < / UL>        值是{{值}}
    < /身体GT;
< / HTML>


解决方案

另一种方式 - 你可以用$父父访问范围

 <输入类型=数字NG-模式=$父$ parent.value/>

请参阅例如: http://jsfiddle.net/7Hh2R/

I am assuming a scope problem is the reason the input in this example can modify the value next to it, but not the other value. If this is the case, how do I hook up the model to right scope? If it isn't a scope problem, what am I doing wrong?

<html ng-app>
    <head>
        <script type="text/javascript" src="angular-1.0.1.min.js"></script>
        <script type="text/javascript">
            function ExampleCtrl($scope) {
                $scope.list = [
                    { name: "a" },
                    { name: "b" },
                ];

                $scope.value = 5;
            }
        </script>
    </head>
    <body ng-controller="ExampleCtrl">
        <ul ng-repeat="item in list">
            <li>{{ item.name }}
                <ng-switch on="item.name">
                    <span ng-switch-when="b">
                        <input type="number" ng-model="value" />
                        {{ value }}
                    </span>
                </ng-switch>
            </li>
        </ul>

        value is {{ value }}
    </body>
</html>

解决方案

Another way - you can access parent scope using $parent.

<input type="number" ng-model="$parent.$parent.value" />

See example: http://jsfiddle.net/7Hh2R/

这篇关于为什么不输入修改NG-重复(和NG-开关)之外的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆