2016-01-24 203 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