2016-01-24 202 views
1

所以經過插件初始化我使用MaterializeCSS,並有這段代碼:AngularJS NG重複

<select> 
<option value="" disabled selected>Jump To Season</option> 
<option option-item ng-repeat="item in series.seasons" value="{{item.id}}"> 
{{item.name}} 
</option> 
</select> 

和IM使用此指令:

.directive("optionItem", [function() { 
return { 
    restrict: 'A', 
    link: function (scope, element) { 
     if (scope.$last) { 
      $('select').material_select(); 
     } 
    } 
}; 
}]); 

現在的元素並重復多次根據需要,但不是顯示$ scope.item.name值,而是將{{item.name}}顯示爲純文本,而不是表達式值。我如何克服這個挫折?我嘗試過使用Material Angular md-select,但是我也遇到過它的問題,每次點擊選擇我都無法點擊網頁上的任何內容,也沒有任何反應。


重要提示: 的series.seasons陣列通過AJAX調用取出。


+0

看到這個:http://stackoverflow.com/questions/12371159/how-to-get-evaluated-attributes-在一個自定義指令 – Soggiorno

+0

重複你在http://jsfiddle.net/ – vorant

回答

0

建議您嘗試使用$timeout(),看是否移動到下一個消化週期有助於

.directive("optionItem", ['$timeout',function ($timeout) { 
return { 
    restrict: 'A', 
    link: function (scope, element) { 
     if (scope.$last) { 
      $timeout(function(){ 
       element.parent().material_select(); 
      }); 
     } 
    } 
}; 
}]); 

不過請注意,你可能還需要需要ngModel控制器和管理模式從插件事件

內改變自己

如果不這樣做,創建一個帶有所需資源的plunker演示來複制此代碼

+0

有問題你的代碼我得到了類似的結果,而不是以純文本顯示的表達式,我得到空選項。 – Bodokh

1

set option-item指令上select

<select option-item> 
     <option value="" disabled selected>Jump To Season</option> 
     <option ng-repeat="item in users" value="{{item.id}}"> 
      {{item.name}} 
     </option> 
    </select> 

和變更指令

.directive("optionItem", ['$timeout', function ($timeout) { 
    return { 
     restrict: 'A', 
     link: function (scope, element) { 
      $timeout(function() { // wait ng-repeat 
       $('select').material_select(); 
      }) 
     } 
    }; 
}]); 

http://jsfiddle.net/vorant/q9bkrzfo/5/

+0

如果它對您有幫助,請將答案標記爲正確。 – vorant

+0

嗨,恐怕我仍然得到正確數量的選項,但是沒有內容。我爲我的問題添加了一個註釋,這些項目通過ajax調用來檢索。 (如果我將它們顯示爲常規選擇,那麼ajax調用肯定會起作用)。 – Bodokh