【发布时间】:2015-10-20 21:38:01
【问题描述】:
我正在尝试编写一个 Angular 指令,该指令将为类似于 http://codepen.io/rachsmith/pen/BNKJme 的单词列表设置动画。但是,我需要从 json 文件加载文本,然后选择一个随机句子来应用动画。我有这部分工作,但无法访问指令的子元素。我假设这是因为在渲染元素之前调用了指令,但是使用 scope.$on($viewContentLoaded, function... 并没有产生任何影响。
我有 jQuery 和 Underscore 可用。
这是我的代码:
控制器
Data.sentences().then(function (response) {
var sentences = response.data;
$scope.sentence = _.sample(sentences);
});
查看
<div class="rotator">
<p>{{sentence.static}}</p>
<text-rotator>
<span class="word" ng-repeat="item in sentence.options">{{item}}</span>
</text-rotator>
</div>
指令
app.directive('textRotator', function () {
return {
restrict: 'E',
link: function (scope, el, attrs) {
var words = el.children('.word');
//cannot access array of items with class of word
}
};
});
【问题讨论】:
-
您是否设法让指令按您的意愿工作?
标签: angularjs animation directive