如何访问ng-repeat作用域?

How can I access to the ng-repeat scope?

本文关键字:ng-repeat 作用域 访问 何访问      更新时间:2023-09-26

我的ng-repeat创建了不同的元素,我需要做一些操作,比如只在ng-repeat范围上创建变量。

如何检索ng-repeat作用域?
我怎么能做这样的事?

<div ng-repeat="item in items">
   <button onclick="load(item, $scope)"></button>
</div>

$scope只能是ng-repeat范围。这可能吗?


编辑

这就是问题所在:

代码:

HTML

<div ng-repeat="item in items" class="container">  
    <div class="row">
        Name: {{item.name}}  
        <select ng-model="item['id']"
        ng-options="opt for opt in ids"
        ng-change="loadTypes(item)"
        >
            <option value="">Default</option>            
        </select>
        <select ng-model="item['type']"
        ng-options="opt for opt in types"
        >
            <option value="">Default</option>
        </select>
   </div>
</div>
控制器

//Executed one time when module is loaded
$scope.init = function(){
    //All ids on the DB        
    $scope.ids = getIdsFromServer();
}
//Executed when an Ids select changes
$scope.loadTypes = function(item){
    //Possible types for an Id
    types = getTypesFromServer(item.id);
    /*
    thisItemNgRepeatScope.types = getTypesFromServer(item.id) ?!??!! Something like this?
    */
}

问题:

id在整个文档中都是一样的,没关系,它可以正常工作。

但是我想只在ng-repeat作用域中更改第二个select (types)的模型。当我改变Id时,我得到这个Id的所有可能类型,但只在我所在的行,选择改变的行。我该怎么做呢?

load(item)函数中强大的'this'是作用域

我假设"ng-repeat作用域"指的是每个单独的ng-repeat循环的父作用域。这可以很容易地通过引用当前作用域的父作用域来实现,像这样:

$scope.$parent