Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 访问重复范围_Angularjs - Fatal编程技术网

Angularjs 访问重复范围

Angularjs 访问重复范围,angularjs,Angularjs,我想在用户单击同级按钮时清除附加到ngRepeat作用域的文本框属性。你会怎么做 ngRepeat似乎没有暴露其范围。。否则我将传递addCustomCheckbox函数 部分 <div ng-repeat="template in currentUser.templates"> <div class="form-group"> <h4>Custom Checkboxes</h4>

我想在用户单击同级按钮时清除附加到ngRepeat作用域的文本框属性。你会怎么做

ngRepeat似乎没有暴露其范围。。否则我将传递addCustomCheckbox函数

部分

<div ng-repeat="template in currentUser.templates">

        <div class="form-group">
            <h4>Custom Checkboxes</h4>
            <ul class="list-group">
                <li ng-repeat="cc in template.customCheckboxes" class="list-group-item">{{cc}}</li>
            </ul>
            <input type="text" ng-model="newCustomCheckboxName"/>
            <button ng-click="addCustomCheckbox(template,newCustomCheckboxName)" class="btn btn-primary">Add Checkbox</button>
        </div>
</div>

试着改变你的想法。不要考虑如何在ng repeat中访问作用域,而要考虑如何将要访问的数据移出作用域

假设您的$scope包含这个ng repeat,它有一个名为'model'($scope.model)的$scope属性。然后,我们可以在$scope.model对象上设置输入模型,而不是将其保留在ng repeat范围内的对象上

我的示例假设“cc”对于每个迭代都是唯一的

<div ng-repeat="template in currentUser.templates">

        <div class="form-group">
            <h4>Custom Checkboxes</h4>
            <ul class="list-group">
                <li ng-repeat="cc in template.customCheckboxes" class="list-group-item">{{cc}}</li>
            </ul>
            <input type="text" ng-model="model[cc]"/>
            <button ng-click="addCustomCheckbox(template,cc)" class="btn btn-primary">Add Checkbox</button>
        </div>
</div>

$scope.addCustomCheckbox = function(template,cc){
    var data = $scope.model[cc];
    if(data==="") return;

    if(_.find(template.customCheckboxes,data)===undefined){
        template.customCheckboxes.push(data);
        $scope.model[cc] = "";
    }

}

自定义复选框
  • {{cc}
添加复选框 $scope.addCustomCheckbox=函数(模板,cc){ var data=$scope.model[cc]; 如果(数据==“”)返回; if(u.find(template.customcheckbox,data)==未定义){ 模板。自定义复选框。推送(数据); $scope.model[cc]=“”; } }
好主意。我最终使用“模板”的标识作为模型查找的键,因为在我的示例中,自定义复选框位于按钮下方一级的范围内。。。因此ng model=“model[template.\u id]”成功了。
<div ng-repeat="template in currentUser.templates">

        <div class="form-group">
            <h4>Custom Checkboxes</h4>
            <ul class="list-group">
                <li ng-repeat="cc in template.customCheckboxes" class="list-group-item">{{cc}}</li>
            </ul>
            <input type="text" ng-model="model[cc]"/>
            <button ng-click="addCustomCheckbox(template,cc)" class="btn btn-primary">Add Checkbox</button>
        </div>
</div>

$scope.addCustomCheckbox = function(template,cc){
    var data = $scope.model[cc];
    if(data==="") return;

    if(_.find(template.customCheckboxes,data)===undefined){
        template.customCheckboxes.push(data);
        $scope.model[cc] = "";
    }

}