ng-repeat无法识别范围数据

时间:2015-05-18 20:20:46

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

我已经阅读过Angular中的ng-repeat和表格,但我仍然无法弄明白。

我的HTML代码如下:

<div class="container">
<h3>table</h3>
    <table>
        <tr ng-repeat="car in userCars">
            <td>{{ car }}</td>
        </tr>
    </table>
</div>

我的控制器看起来像那样:

app.controller('ManageCarsController', function ($scope, $location, UserService) {
    'use strict';
    (function () {
        console.log('code is getting here');
        $scope.userCars = [1,2,3];
        $scope.UserService = UserService;
    })();

它会留下一张空表。 但是如果我将代码更改为这样,它可以正常工作(但这对我的使用不利):

app.controller('ManageCarsController', function ($scope, $location, UserService) {
    'use strict';
    $scope.userCars = [1,2];
    (function () {
        $scope.UserService = UserService;
    })();

1 个答案:

答案 0 :(得分:1)

我不认为IIFE(立即调用的函数表达式)可以帮助您。你可能应该完全删除它,这反过来会使一切正常对你

app.controller('ManageCarsController', function ($scope, $location, UserService) {
$scope.userCars = [1,2];
$scope.UserService = UserService;

如果你真的想使用IIFE,你应该将整个代码放在其中

'use strict';
(function () {
app.controller('ManageCarsController', function ($scope, $location, UserService) {
'use strict';
$scope.userCars = [1,2];
    $scope.UserService = UserService;
})();