无法访问ng-if父级的范围

时间:2014-01-24 16:20:56

标签: javascript json angularjs

我有以下ng-repeat

<div ng-repeat="goal in goals">
    {{ goal }}
    {{ goal.bodyGoalId }}
</div>

这会按预期输出目标和bodyGoalId

然而

以下

<div ng-repeat="goal in goals">
    <div ng-if="goal.bodyGoalId !== null">
        {{ goal.bodyGoalId }}
    </div>
</div>

创建了if div,如果条件反转,则不会创建。无论我做什么尝试输出bodyGoalId然而没有输出。我想这与ng-if创建的新范围有关,但我认为ng-if继承了它的父范围,但我不能正确理解它。

使这项工作的正确方法是什么?

EDIT1:

所以上面的内容不适用于ng-show,因此它不能成为范围问题。这是我尝试访问的对象后面的实际代码。请注意,%% %%与此代码中的{{}}等效

<table class="col-xs-12 table table-striped table-bordered table-hover">

    <tr>

        <th scope="col" class=""><h4>Created</h4></th>
        <th scope="col" class=""><h4>Type</h4></th>
        <th scope="col" class=""><h4>Targets</h4></th>

    </tr>

    <div ng-repeat="goal in goals">

        <tr ng-show="goal.distanceGoalId !== null">
            <th>%% goal.distanceGoalId %%</th>
            <th>Distance</th>

            <th>
                Excersise: %% goal.distance_goal_desc.excersise.name %%</br>
                Distance: %% goal.distance_goal_desc.distance %%</br>
                Time: %% goal.distance_goal_desc.time %%
            </th>
        </tr>
   </div>
</table>

和%%目标%%对象输出

{
    "id":"1",
    "userId":"1",
    "title":"first goal title",
    "goalDesc":"This should describe my goal in text form",
    "goalStatus":"0",
    "bodyGoalId":null,
    "strengthGoalId":null,
    "distanceGoalId":"1",
    "created_at":"2014-01-24 12:11:40",
    "updated_at":"2014-01-24 12:11:40",
    "body_goal_desc":null,
    "distance_goal_desc":
        {"id":"1","excersise":
            {"id":"1","name":"Leg Extension",
                "type":"Resistance",
                "variation":null,
                "equipment":"Machine",
                "focus":"Legs",
                "desc":"",
                "video":null,
                "created_at":"2014-01-24 12:11:41",
                "updated_at":"2014-01-24 12:11:41"
             },
         "distance":"9.50",
         "time":"01:22:00",
         "created_at":"2014-01-24 12:11:40",
         "updated_at":"2014-01-24 12:11:40"
        },
     "strength_goal_desc":null}

控制器

myApp.controller('goalCtrl', ['$scope', '$http', function ($scope, $http) {
    $scope.goals = null;

    $scope.getGoals = function (userid) {
        $http.get('/goals/' + userid).success(function (goals, status) {
            if (!isEmpty(goals)) {
                $scope.goals = goals;
            };
        });
    };
}]);

编辑2:如果我从表中删除ng-repeat div,则值显示为正常

0 个答案:

没有答案