简体   繁体   English

在ng-repeat之外创建一个新范围

[英]Create a new scope outside ng-repeat

The idea is how a list of texts can be modified by pressing the button next to the text. 这个想法是如何通过按下文本旁边的按钮来修改文本列表。 We can also apply it to the title text which is outside the list. 我们也可以将它应用于列表之外的标题文本。

HTML: HTML:

<div ng-controller="TextController">
    <div class="title">
        <span>{{ text }}</span>
        <button ng-click="edit()">Edit</button>
    </div>

    <ul>
        <li ng-repeat="text in list">
            <span>{{ text }}</span>
            <button ng-click="edit()">Edit</button>
        </li>
    </ul>
</div>

JavaScript: JavaScript的:

angular.module("app").
    controller("TextController", function($scope) {
        $scope.text = "hello";
        $scope.list = [....]; // list of texts;

        $scope.edit = function() {
            this.text += " world";
        };
    });

I'm not sure if I wrote it the right way. 我不确定我是否以正确的方式写它。 However, everything works fine except the edit button in the title which is when I'm trying to edit the title only, it accidentally edits all text which is in its children scope. 但是,一切正常,除了标题中的编辑按钮,当我试图仅编辑标题时,它意外地编辑了其子范围内的所有文本。

What I'm trying to do is to give the title a new scope so that the button doesn't affect other texts because it isn't a parent of any scope. 我要做的是给标题一个新的范围,以便按钮不会影响其他文本,因为它不是任何范围的父级。

Why don't you use another variable name for ng-repeat(i have made second text => txt). 为什么不为ng-repeat使用另一个变量名(我已经制作了第二个text => txt)。 It is better to have separate functions for updating list and text, but if you want, you can try 最好有单独的功能来更新列表和文本,但如果你愿意,你可以试试

<div ng-controller="TextController">
    <div class="title">
        <span>{{ text }}</span>
        <button ng-click="edit()">Edit</button>
    </div>

    <ul>
        <li ng-repeat="txt in list">
            <span>{{ txt }}</span>
            <button ng-click="edit($index)">Edit</button>
        </li>
    </ul>
</div>

The view is passing $index of the array element: 视图传递数组元素的$ index:

    $scope.edit = function(listIndex) {
        if(listIndex) $scope.list[listIndex] += " world"
        else $scope.text += " world";
    };

Yeah What you are trying to do is right: 是的你想要做的是对的:

The {{text}} variable is bound to the same controller scope. {{text}}变量绑定到同一控制器范围。 so the edit button just updates that value which makes it to change everywhere 因此编辑按钮只会更新该值,使其随处更改

You should try to update $scope.list value: When you updated the scope, the html will render. 您应该尝试更新$ scope.list值:更新范围时,html将呈现。

$scope.edit = function() {
    $scope.list[this.$index] +=  " world";
};

Please check the demo: http://jsfiddle.net/Lvc0u55v/7050/ 请查看演示: http//jsfiddle.net/Lvc0u55v/7050/

You could create an "include" combined with an ng-template so that you get a new scope. 您可以创建一个与“ng-template”结合使用的“include”,以便获得新的范围。

http://jsfiddle.net/pfeq0mwe/3/ http://jsfiddle.net/pfeq0mwe/3/

<div ng-app="myApp" ng-controller="myCtrl">

<div ng-include src="'myTemplate.htm'">

</div>

    <ul>
        <li ng-repeat="text in list">
            <span>{{ text }}</span>
            <button ng-click="edit()">Edit</button>
        </li>
    </ul>


    <script type = "text/ng-template" id = "myTemplate.htm">
     <div class="title">
        <span>{{ text }}</span>
        <button ng-click="edit()">Edit</button>
    </div>
   </script>

</div>

<div >

</div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM