访问NG重复内部模型 [英] Accessing the model inside a ng-repeat

查看:91
本文介绍了访问NG重复内部模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NG-重复遍历流程。我所要做的是通过与选择框的表单中添加一个新的来的电流过程。不过,我的问题是我无法从控制器访问内循环模式。我想这是因为一个新的范围在 NG-重复循环创建。

不过我还是不能找到一种方法,从控制器访问模型。这里是HTML和JavaScript code片,为您更好地理解问题。

 < D​​IV CLASS =容器NG-控制器=ProcessCtrl>
    < D​​IV CLASS =过程NG重复=过程,过程>
        <窗​​体类=形式的内联NG提交=addBlock($指数)>
            <选择NG模型=blockType>
                <期权价值=1>&TYPE1 LT; /选项>
                <期权价值=2> 2型< /选项>
                <期权价值=3> 3型< /选项>
            < /选择>
            <按钮式=提交>添加< /按钮>
        < /表及GT;
    < / DIV>
< / DIV>

angularjs控制器

 函数ProcessCtrl($范围,$过滤器){
    // ...    $ scope.addBlock =功能(指数){
        警报($ scope.blockType); //未定义
        $ scope.processes [指数] .blocks.push({类型:$ scope.blockType});
    };
}


解决方案

是的,问题是父范围无法访问由NG-重复创建的子范畴。

修改addBlock也传递了blockType:

  NG-提交=addBlock($指数,blockType)

I have a ng-repeat loop over processes. What I am trying to do is to add a new block to the current process via a form with select box. However, my problem is I cannot access the model inside the loop from the controller. I guess this is because a new scope is created in a ng-repeat loop.

Still I couldn't find a way to access model from controller. Here is html and javascript code pieces for you to understand problem better.

<div class="container" ng-controller="ProcessCtrl">
    <div class="process" ng-repeat="process in processes">
        <form class="form-inline" ng-submit="addBlock($index)">
            <select ng-model="blockType">
                <option value="1">type1</option>
                <option value="2">type2</option>
                <option value="3">type3</option>
            </select>
            <button type="submit">add</button>
        </form>
    </div>
</div>

angularjs controller

function ProcessCtrl($scope, $filter) {
    //...

    $scope.addBlock = function(index) {
        alert($scope.blockType); // undefined
        $scope.processes[index].blocks.push({type: $scope.blockType});
    };
}

解决方案

Yes, the problem is that the parent scope can not access the child scopes created by ng-repeat.

Modify addBlock to also pass up the blockType:

ng-submit="addBlock($index, blockType)"

这篇关于访问NG重复内部模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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