2015-06-09 67 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>