2013-11-24 62 views
4

我有一個問題與angular-ui/select2控制。問題與預填充角UI的select2

我想使用angularjs到預先用對象數組填充控件。我用的是init函數,以試圖實現這一點,但不知何故,認爲沒有更新頁面上...

這裏客戶端模塊

angular.module('searchEngineModule', ['ng', 'languageChooserModule','geolocationModule','currentMemberAddressModule', 'addressAutocompleteModule']) 
.factory('searchEngineService', function(){ 

}) 
.controller('searchEngineCtrl', [ '$scope', '$http', 'languageChooserService', 'retrieveDefaultLanguagesService', 'geolocationService', 'currentMemberAddressService', 'addressAutocompleteService','addressFromReferenceService', function geolocationCtrl($scope, $http, languageChooserService, retrieveDefaultLanguagesService, geolocationService, currentMemberAddressService, addressAutocompleteService, addressFromReferenceService) { 

     $scope.searchCriteria = {}; 

     $scope.languageChooser = languageChooserService; 

     $scope.addressAutocomplete = addressAutocompleteService; 

     $scope.init = function() { 

      retrieveDefaultLanguagesService.defaultLanguages().then(function(languages){ 
       $scope.searchCriteria.languages = [{}]; 
       $scope.searchCriteria.languages= languages;//HERE: it does populate the model but the view is not updated...  
      }); 

      geolocationService.geolocationAddress().then(function(address) { 
       $scope.geolocationAddress = {}; 
       $scope.geolocationAddress = address; 
      }); 

      currentMemberAddressService.currentMemberAddress().then(function(address){ 
       $scope.currentMemberAddress = {}; 
       $scope.currentMemberAddress = address; 
      }); 

     }; 

     $scope.$watch('addressAutocomplete', function (newVal, oldVal) { 
      if (oldVal == newVal) return; 
      $scope.onTheFlyAddress = {}; 
      if(newVal){ 
       addressFromReferenceService.addressFromReference(newVal.reference).then(function(address){ 
        $scope.onTheFlyAddress = address; 
       }); 
      } 
     }, true); 

     $scope.performSearch = function(){ 
      console.log('performSearch'); 
      console.log($scope.searchCriteria); 
     };  
}]) 
.config(function($httpProvider) { 
    $httpProvider.defaults.headers.common['Content-Type'] = 'application/json'; 
    $httpProvider.defaults.headers.common['X-Ajax'] = 'true'; 
}); 

這裏是languageChooserModule

angular.module('languageChooserModule', ['ng', 'ui.select2']) 
.factory('languageChooserService', function(){ 
    return select2Options(); 
}) 
.factory('retrieveDefaultLanguagesService', ['$http', '$q', function($http, $q){ 
    function retrieveDefaultLanguagesP(){ 
     var deferred = $q.defer(); 
     var defaultLanguages = [{}]; 
     $http.get('/bignibou/utils/findLanguagesByLanguageStartingWith.json', {params:{language: 'fran'}}) 
     .success(function(languages){ 
      defaultLanguages = languages; 
      deferred.resolve(defaultLanguages); 
     }); 
     return deferred.promise; 
    } 

    return{ 
     defaultLanguages: function(){ 
      return retrieveDefaultLanguagesP(); 
     } 
    }; 
}]); 

function select2Options(){ 

    function format(item) { 
     return item.description; 
    } 

    return {  
     simple_tags: false, 
     multiple : true, 
     contentType: "application/json; charset=utf-8", 
     minimumInputLength : 3, 
     data:{ text: "description" }, 
     formatSelection: format, 
     formatResult: format, 
     ajax : { 
      url : "/bignibou/utils/findLanguagesByLanguageStartingWith.json", 
      dataType : 'json', 
      data : function(term) { 
       return { 
        language : term 
       }; 
      }, 
      results : function(data, page) { 
       return { 
        results : 
         data.map(function(item) { 
          return { 
           id : item.id, 
           description : item.description, 
           version : item.version 
          }; 
         } 
       )}; 
      } 
     } 
    }; 
} 

任何人都可以幫忙嗎?

編輯1

換款爲以下:

retrieveDefaultLanguagesService.defaultLanguages().then(function(languages){ 
       $scope.searchCriteria.languages = [{}]; 
       $scope.searchCriteria.languages= languages; 
       $scope.$digest(); 
      }); 

導致以下錯誤:

Error: [$rootScope:inprog] $digest already in progress 
http://errors.angularjs.org/1.2.1/$rootScope/inprog?p0=%24digest 
    at http://localhost:8080/bignibou/js/libs/angular.js:78:12 
    at beginPhase (http://localhost:8080/bignibou/js/libs/angular.js:11878:15) 
    at Scope.$digest (http://localhost:8080/bignibou/js/libs/angular.js:11412:9) 
    at Scope.$delegate.__proto__.$digest (<anonymous>:844:31) 
    at http://localhost:8080/bignibou/js/custom/searchEngineModule.js:18:12 
    at wrappedCallback (http://localhost:8080/bignibou/js/libs/angular.js:10597:81) 
    at http://localhost:8080/bignibou/js/libs/angular.js:10683:26 
    at Scope.$eval (http://localhost:8080/bignibou/js/libs/angular.js:11576:28) 
    at Scope.$digest (http://localhost:8080/bignibou/js/libs/angular.js:11421:31) 
    at Scope.$delegate.__proto__.$digest (<anonymous>:844:31) 

編輯2

更改爲第Ë以下:

$scope.$apply(function(){ 
    retrieveDefaultLanguagesService.defaultLanguages().then(function(languages){ 
        $scope.searchCriteria.languages= languages; 
       }); 
      }); 

導致以下錯誤:

Error: [$rootScope:inprog] $apply already in progress 
http://errors.angularjs.org/1.2.1/$rootScope/inprog?p0=%24apply 
    at http://localhost:8080/bignibou/js/libs/angular.js:78:12 
    at beginPhase (http://localhost:8080/bignibou/js/libs/angular.js:11878:15) 
    at Scope.$apply (http://localhost:8080/bignibou/js/libs/angular.js:11675:11) 
    at Scope.$delegate.__proto__.$apply (<anonymous>:855:30) 
    at Scope.$scope.init (http://localhost:8080/bignibou/js/custom/searchEngineModule.js:17:11) 
    at http://localhost:8080/bignibou/js/libs/angular.js:9885:21 
    at Scope.$eval (http://localhost:8080/bignibou/js/libs/angular.js:11576:28) 
    at pre (http://localhost:8080/bignibou/js/libs/angular.js:18210:15) 
    at nodeLinkFn (http://localhost:8080/bignibou/js/libs/angular.js:6104:13) 
    at compositeLinkFn (http://localhost:8080/bignibou/js/libs/angular.js:5536:15) 
+0

請問您可以發佈您的html代碼嗎?有可能它有一些問題,而不是你的JavaScript。 – Dusty

回答

4

如果從retrieveDefaultLanguagesService.defaultLanguages()您的返回值是一個$q.defer().promise然後(哈哈!)then會導致消化發生,因此$apply,所以你編輯是多餘的。如果你需要做的是,在未來(通常是罕見的),你應該這樣來做:

if(!rootScope.$$phase)rootScope.$apply(); 

爲了減少一些複雜我也建議刪除的searchCriteria初始化和你then成功回調中初始化的對象結構。像這樣:

retrieveDefaultLanguagesService.defaultLanguages().then(function(languages){ 
    $scope.searchCriteria = {languages:languages}; 

}); 

如果這不起作用,我可能會猜測你的html在某些方面不正確。如果你分享它,你可能會找到更多的幫助。

我還使用angluarjs 1.2.3和ui-select2沒有問題

1

我忘了提及,我使用的角度1.2.1和根據此信息:(https://stackoverflow.com/a/20186141/536299)似乎有角之間的不兼容JS 1.2和角度UI選擇2 ....

+1

我正要提到這一點。這是一個「功能」,因爲它在Angular UI中破壞了某些東西而被放棄。最後的1.2下降了這個功能,因此我的回答對你不起作用。 –