2016-05-09 79 views
1

我想實現角ui.select到我的項目使用引導主題,但我沒有什麼,但它的問題。我正在傳遞一些非常簡單的物品。 這裏是我試圖通過角ui.select不呈現項目

[Object { institution_type_id="1", institution_type_description="Post"}, Object { institution_type_id="2", institution_type_description="Security"}, Object { institution_type_id="3", institution_type_description="Mutual Fund"}, Object { institution_type_id="4", institution_type_description="Bank"}, Object { institution_type_id="5", institution_type_description="Life insurance"}] 

它使用的是常規選擇,如下圖所示呈現的微小物體。

Regular Select

我想使用的角度,而不是ui.select,這裏是我的設置.. 我已經包含在我的主網頁下面

<!-- Angular ui-select --> 
    <link rel="stylesheet" href="../bower_components/ui-select/dist/select.min.css" type="text/css" /> 

<!-- Angular ui-select --> 
<script src="../bower_components/ui-select/dist/select.min.js"></script> 
<script src="../bower_components/angular-sanitize/angular-sanitize.min.js"></script> 

我想用bootstrap主題所以當然引導CSS也被包括在我的app.js

現在,我已經包括了

ui.select

的依賴

這是我在控制器:

institutionService.getAllInstitutionTypes().then(function(data){  
     $scope.institutionTypes = data; 
    }); 

,這裏是我的html的模樣。

<div class="form-group required" ng-class="{ 'has-error': addEditInstitutionForm.institution_type_id.$invalid && addEditInstitutionForm.institution_type_id.$dirty}"> 
       <label for="institution_type_id" class="control-label col-xs-4">Institution Type</label> 
       <div class="col-xs-8"> 
        <ui-select ng-model="ctrl.country.selected" theme="bootstrap" style="width: 300px;" title="Choose Institution Type"> 
         <ui-select-match placeholder="Select or search institutionType...">{{$select.selected.institution_type_description}}</ui-select-match> 
         <ui-select-choices repeat="type in institutionTypes"> 
          <span ng-bind-html="type.institution_type_description"></span> 
          <small ng-bind-html="type.institution_type_id"></small> 
         </ui-select-choices> 
        </ui-select> 
       </div> 

當我加載頁面時,它看起來像ui.select已呈現罰款..但下拉內沒有任何內容,如下所示。 angular ui.select dropdown

我在控制檯中看到很多關於$ sce的錯誤:不安全,如下所示。 $sce:unsafe error

問題現在:

  1. 我怎樣才能修復這些錯誤,以正確地呈現下拉。
  2. 在代碼片段(http://angular-ui.github.io/ui-select/demo-basic.html)中,他們引用了幾個地方的$ select。有人能幫我理解嗎?
  3. 我如何驗證角度ui.select,如果我設法解決問題並更好地理解它,以便在我的項目中使用它。
  4. 有沒有比angular.ui選擇更好,更簡單的選擇?

謝謝。

+0

你是嗎包括某處的某個外部URL?這主要是顯示錯誤的原因。 – Matheno

+0

解決您的問題[這裏](http://stackoverflow.com/questions/18340872/how-do-you-use-sce-trustashtmlstring-to-replicate-ng-bind-html-unsafe-in-angu) –

+0

不,我沒有在外部加載任何東西。所有的CSS和JS文件都從項目文件夾中加載 –

回答

0

幾個問題。

  1. 需要注入ngSanitize,像這樣:

var app = angular.module('plunker', ['ui.select', 'ngSanitize']);

  • 您正在使用您的NG-模型ControllerAs模式,「ctrl.country 。選擇」,但不引用在重複屬性的選項陣列中,當 「類型中institutionTypes」
  • 控制器:

    var app = angular.module('plunker', ['ui.select', 'ngSanitize']); 
    
    app.controller('MainCtrl', function($scope) { 
    
        var self = this; 
    
        self.institutionTypes = [{ 
        institution_type_id: "1", 
        institution_type_description: "Post" 
        }, { 
        institution_type_id: "2", 
        institution_type_description: "Security" 
        }, { 
        institution_type_id: "3", 
        institution_type_description: "Mutual Fund" 
        }, { 
        institution_type_id: "4", 
        institution_type_description: "Bank" 
        }, { 
        institution_type_id: "5", 
        institution_type_description: "Life insurance" 
        }]; 
    
        self.country = { 
        institution_type_id: "5", 
        institution_type_description: "Life insurance" 
        }; 
    

    HTML:

     <ui-select ng-model="ctrl.country" theme="bootstrap"> 
         <ui-select-match placeholder="Select or search institutionType...">{{$select.selected.institution_type_description}}</ui-select-match> 
         <ui-select-choices repeat="type in ctrl.institutionTypes | filter: $select.search "> 
          <span ng-bind-html="type.institution_type_description | highlight: $select.search"></span> 
         </ui-select-choices> 
         </ui-select> 
    

    });

    這是一個工作plunker

    +0

    bBrown,我已經修復在ng-model中的問題是institutionTypes.selected,並且除了現在我什麼都不能引起任何其他選擇。感謝您指點我..我的jQwidgets分離器面板還有另一個問題,它的高度並未調整到ui select元素,因爲它是在事實之後呈現的。所以現在它呈現得很好。我怎樣才能驗證ui選擇一個空值,只要我標籤出來。另外我需要在表單提交上驗證它。 –

    +0

    @AndyJohnson - 如果您有其他問題,您應該單獨提交這些問題,而不是將此問題運行到其他問題中。 – jbrown