ng-repeat中的指令范围问题

问题描述 投票:1回答:1

即使经过一周的深度试验,我也无法解决以下问题。

1)容器的Html在E指令中使用带有ng-repeat的Html容器,这是一个简化的例子。

<div ng-repeat="block in blocks">
   <my-directive
    selected="selected[block.name]"
    block-name="block.name"
   >
   </my-directive>
</div>

2)指令代码

module.directive('myDirective', myDirective);

function myDirective() {
    return {
        restrict: 'E',
        templateUrl : 'myDirectiveTemplate.tpl.html',
        scope: {
            selected: '='
            blockName: '='
        },
        link: function ($scope) {
        }
    }
};

3)使用的指令的Html

<div>
 <div>
  <input type="text" ng-model="selected.denomination" myFunc="onCompletedListClick($parent.selected, $parent.blockName)"
   class="form-control"/>
 </div>
</div>
<button
ng-click="onCompletedListClick($parent.selected, $parent.blockName)">
                    ReSearch</button>

问题出在指令上,当使用按钮时,示波器始终使用正确的数据,在这种情况下按下Enter键时会调用myFunc。

我在某些情况下让我们说四个区块,当做第一个的研究是好的,但是对第二个进行研究它是使用第一个的范围...但只有当按键时,研究从按钮开始好像总是好的。

我的问题是:1)如何解决这个问题,我对此感到疯狂2)为什么会这样?

angularjs angularjs-scope
1个回答
0
投票

发现了这个问题。

真正的问题是缺少Button的类型,表单中打印的字段具有按下Enter键时提交的“默认”行为。没有类型的按钮的默认值是“submit”,所以它试图提交表单,并且启动了没有类型的第一个按钮,我修复了按钮并在我的指令上放置了ng-keydown函数正确处理了Enter键提交。

© www.soinside.com 2019 - 2024. All rights reserved.