angularjs - Angular $scope.model 不会绑定(bind)到 ng-repeat

标签 angularjs scope ng-repeat

angular.module('demoapp').controller("EventControllerNearPosition", EventControllerNearPosition);
EventControllerNearPosition.$inject = ["$scope", "ApiEventFactory", "ApiPositionFactory", "UserInteractionMessagesFactory", "$location"]

function EventControllerNearPosition($scope, apiEvent, apiPosition, UIMfactory, $location){
UIMfactory.printUserSuccessMessages();
UIMfactory.printUserFailedMessage();

getAllPositions();

function getAllPositions() {
    apiPosition.getAllPositions().then(function(data){
        $scope.positions = data.requested_positions;
    }).error(function(error){
        UIMfactory.addUserFailedMessage("Something went wrong, try again!");
        $location.path("/");
    });
};

$scope.SearchEvent = function(){
    apiEvent.showNearbyEvents($scope.position_id).then(function(nearEvents){
        $scope.events = nearEvents;
    }).error(function(error){
        UIMfactory.addUserFailedMessage("Something went wrong when fetching the events, try again!");
        $location.path("/");
    });
};
};

Angular 代码^

<div class="event-container">
<div>
    <h3>Ange position</h3>
    <select id="position" data-ng-model="position_id">
        <option data-ng-repeat="position in positions" value="{{position.id}}">
            {{position.location_name}}
        </option>
    </select>
</div>
<div class="form-group">
    <input type="submit" class="btn btn-default" data-ng-click="SearchEvent()" value="Skicka"/>
</div>

选择不会绑定(bind)我从 api 获得的位置。我已经调试了应用程序并检查了我是否得到了正确的值。我知道相似或“相等”的代码在其他部分 View 中有效,所以我不明白为什么它在这里不起作用。而且当我按下按钮将触发“SearchEvent”时,调试器中没有任何反应。如果这很重要,我会使用 firefox

感谢任何类型的反馈!

最佳答案

我只能看到一个错误,你应该从 .then 函数的响应对象中获取 data

function getAllPositions() {
    apiPosition.getAllPositions().then(function(response){
        //change in below line
        $scope.positions = response.data.requested_positions;
    }, function(error){
        UIMfactory.addUserFailedMessage("Something went wrong, try again!");
        $location.path("/");
    });
};

关于angularjs - Angular $scope.model 不会绑定(bind)到 ng-repeat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37236178/

相关文章:

javascript - ng-repeat 中带有字母表的项目符号列表

drop-down-menu - AngularJS + Bootstrap 下拉: can't do ng-click in ng-repeat

python - 无法在 TensorFlow 中共享/重命名变量

java - 这是对 'this' 关键字的不明智使用吗?

javascript - Angular js Bootstrap 模式错误

javascript - 带数组的反向查找对象

programming-languages - 为什么有些语言要求在调用之前在代码中声明函数?

javascript - 在 ng-repeat 上更改一次迭代

javascript - 返回上一页

javascript - 当我在 Controller 中使用 ui.bootstrap 时,Angular 给出空白页面