【问题标题】:Dynamical ng-model in ng-repeat is undefinedng-repeat 中的动态 ng-model 未定义
【发布时间】:2016-09-04 11:11:31
【问题描述】:

我需要有关 ng-repeat 中动态生成模型的帮助。我有一个带有 ng 重复表行的 HTML。每行都有一个使用更新按钮更新图片名称和价格的选项。

HTML

<tr role="row" class="odd" ng-repeat="i in slideShowImages track by $index">
    <td>
        <img ng-src="{{i.thumb_path}}" width="100px" height="auto" >
    </td>
    <td>
        <div class="form-group">
            <input type="text" name="imageName" ng-model="i.imageName[$index]"
                   class="form-control" id="imageName" required>
        </div>
    </td>
    <td>{{i.date_taken | myDate}}</td>
    <td>
        <div class="form-group">
            <input type="text" name="imagePrice" ng-model="i.imagePrice[$index]"
                   class="form-control" id="imagePrice" required>
        </div>
    </td>
    <td>
        {{i.position}}
    </td>
    <td>
        <a ng-click="updateImage(i.id_thumb, $index)">
            <button class="btn btn-block btn-success">Update</button>
        </a>
    </td>
    <td>
        <a ng-click="deleteImage(i.id_thumb, $index)">
            <button class="btn btn-block btn-danger">Delete</button>
        </a>
    </td>
</tr>

这是我试图获取值的控制器函数

$scope.updateImage = function(id, $index){
    $scope.models = {};
    console.log(id);
    console.log($index);
    console.log($scope.i.imageName[$index]);
    console.log($scope.i.imagePrice[$index]);
};
// result of console logs
4
0
angular.js:13550 TypeError: Cannot read property 'imageName' of undefined
    at Scope.$scope.updateImage (locationsCtrl.js:243)
    at fn (eval at compile (angular.js:14432), <anonymous>:4:486)
    at expensiveCheckFn (angular.js:15485)
    at callback (angular.js:25018)
    at Scope.$eval (angular.js:17229)
    at Scope.$apply (angular.js:17329)
    at HTMLAnchorElement.<anonymous> (angular.js:25023)
    at HTMLAnchorElement.dispatch (jQuery-2.1.4.min.js:3)
    at HTMLAnchorElement.r.handle (jQuery-2.1.4.min.js:3)

我猜我做错了什么,基于 imageName 和 imagePrice 不能未定义的错误。我希望你们能帮助我。如果您需要任何其他信息,请告诉我,我会提供。提前谢谢你

【问题讨论】:

  • 您的 $scope.slideShowImages 是否在其每个对象中都有一个属性 imageName ?
  • 每个对象都包含与 $scope.slideShowImages 中其他对象相同的属性
  • 什么是 $scope.i ??您不能像那样引用范围内的迭代值。您实际上应该寻找 $cope.slideShowImages[$index].imageName[$index]

标签: javascript angularjs angularjs-ng-repeat ng-repeat


【解决方案1】:

你有 ng-repeat 和 i in slideShowImages 这表示,在每次迭代中,i 将只有 UI 上的当前集合元素(不在控制器范围内)。所以你不能在控制器内获得$scope.i 值。您必须将该值从updateImage 作为参数传递,例如ng-click="updateImage(i)"。然后你就可以在 UI 上使用i 对象了。

HTML

<td>
    <a ng-click="updateImage(i)">
        <button class="btn btn-block btn-success">Update</button>
    </a>
</td>
<td>
    <a ng-click="deleteImage(i, $index)">
        <button class="btn btn-block btn-danger">Delete</button>
    </a>
</td>

控制器

$scope.updateImage = function(i){
    console.log(i.imageName);
    console.log(i.imagePrice);
};

【讨论】:

  • i.imageName 中的索引不是必需的,因为您将整个对象作为参数传递给函数。无论如何它都在工作。谢谢
【解决方案2】:

您无法在控制器中获取ng-repeat 的i 对象。 还有什么可以做的是使用 json 中的任何其他键进行过滤。

例子:

$scope.currentImageName = $filter('filter')(slideShowImages,{ id: $index});

这里假设 slideShowImages 有一个字段 id 的值 - $index。

另外,在控制器定义中添加依赖注入$filter。

【讨论】:

  • 与其通过过滤找到 currentImageName,不如在我的答案中显示的方法参数中传递 i 对象,尽管基于$index 的过滤不是一个很好的选择.
  • @Pankaj,是的,这是一个更好的主意。前进。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-26
  • 1970-01-01
  • 2014-10-30
  • 2013-07-10
  • 1970-01-01
相关资源
最近更新 更多