angularjs默认选择始终更改

时间:2015-06-09 05:25:51

标签: javascript angularjs select

使用angularjs选择标签和ng-options,在我选择标签后,我无法获得正确的默认selecedValue

HTML

<select ng-model="selectedStyle" ng-options="style as style.name for style in styles"></select>

的javascript

 $scope.$on('openStyleList', function (event, page) {


                styleListService.Get().
                then(function (data) {
                    var tempArray = new Array();
                    if(page.pageType == 'Jacket')
                    {
                        tempArray.push(_.first(data))
                        $scope.styles = tempArray;
                        alert($scope.styles[0].name)//here i get the first one
                        $scope.selectedStyle = $scope.styles[0];//but here the $scope.selectedStyle is not always the first one! It's always the one that I selected before.I cannot set it to the default one($scope.styles[0])
                    }
                    else if (page.pageType == 'Body') {
                        if (_.rest(data).length == 1) {
                            tempArray.push(_.rest(data))
                            $scope.styles = tempArray;
                        }
                        else {
                            $scope.styles = _.rest(data);
                        }
                        alert($scope.styles[0].name)//aslo here
                        $scope.selectedStyle = $scope.styles[0];//aslo here
                    }

                });

            })

styleListService代码:

angular.module('editorApp')
  .service('styleListService', ['$http', '$log', '$q', '$timeout', '$window', 'baseService', 'settings',
      function styleListService($http, $log, $q, $timeout, $window, baseService, settings) {

          var StyleListServiceFactory = {};

          var _get = function () {

              return baseService.get('styles/');

          }
          StyleListServiceFactory.Get = _get

          return StyleListServiceFactory;
  }]);

baseService代码的一部分:

var _get = function (apiPath, responsetype) {

    var deferred = $q.defer();

    if (responsetype == undefined)
    {
        responsetype = "";
    }

    $http.defaults.cache = false;

    $http.get(settings.baseUrl + apiPath,
        {
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            responseType: responsetype

        }).success(function (response) {

            deferred.resolve(response);

        }).error(function (data, status, headers, config) {

            deferred.reject(status);

        });

    return deferred.promise;
}

baseServiceBaseFactory.get = _get;

1 个答案:

答案 0 :(得分:0)

1) Select your default value as model
you can do this like

<select tabindex="2" class="dropdown-control " id="drpSystemDeliveryCos" ng-model="selecedValue ">
                                                            <option ng-repeat="style in styles" value="{{style .ID}}">{{style .name}}</option>
                                                        </select>

OR
    <select tabindex="6" class="form-control" id="drpOrderType1" ng-options="style.ID as style.name for style in styles " ng-model="selecedValue" ></select>