2015-06-09 64 views
2

angularjs选择选项卡和ng-options,我选择一个后我不能得到正确的默认selecedValueangularjs默认选择总是变化

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

能否请您发布样式阵列的结构? – shreyansh

+0

yes.the样式数组是这样的:{ “name”:“12345”, “pageType”:“Jacket”, “pageTemplateVariationID”:1} @shreya – chii

+0

好像你需要使用'$ apply'或'$用于启动摘要循环的超时时间,在更改用于查看的值之后。无论如何,你可以提供'styleListService.Get'吗?我想它会返回'$ http' – Grundy

回答

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>