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)我从 i api 获得的位置。我已经调试了应用程序并检查我是否得到了正确的值。我知道类似或“相等”的代码在其他部分 View 中工作,所以我不明白为什么它在这里不起作用。而且当我按下按钮时将触发“SearchEvent”,调试器中没有任何 react 。如果这很重要,我正在使用 Firefox

感谢您的任何反馈!

最佳答案

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

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/

10-11 23:52