gpt4 book ai didi

javascript - TimelineJs 和 AngularJs ng-查看内容

转载 作者:行者123 更新时间:2023-12-02 19:04:21 25 4
gpt4 key购买 nike

我在我的项目中使用AngularJs,所以我的应用程序中的所有主要内容都是使用Angular的ng-view指令加载的,我想在我的应用程序中使用TimelineJs实现时间轴,时间轴得到一个id(放置的地方)时间线),但是我的具有该 id 的 div 是通过 Angular 异步加载的,因此当 TimelineJs 脚本运行时 div#id 不存在,知道如何解决这个问题吗?

这是代码:

$(document).ready(function() {
createStoryJS({
type: 'timeline',
width: '800',
height: '400',
source: './timeline.json',
embed_id: 'dash-tim'
});
});

这是错误:

Uncaught TypeError: Cannot call method 'appendChild' of null storyjs-embed.js:45
T storyjs-embed.js:45
createStoryJS storyjs-embed.js:45
(anonymous function) extra.js:21
fire jquery-1.8.2.js:974
self.fireWith jquery-1.8.2.js:1082
jQuery.extend.ready jquery-1.8.2.js:406
DOMContentLoaded

最佳答案

这是我的实现方式。

我有一个 Angular 指令,它调用TimelineJS 代码来修改 dom(我绝对不是 AngularJS 专家,但这似乎是其他人推荐的模式)。

HTML

<div ng-app="StoryUniverse" ng-controller="EventsListCtrl">
<div id="my-timeline" timeline-js></div>
</div>

Angular Directive(指令)

app.directive('timelineJs',  function ($timeout) {
return {
restrict: 'A',
link: function (scope, elem, attrs) {
postpone = $timeout(function() {
createStoryJS({
type: 'timeline',
width: '800',
height: '600',
source: scope.events,
embed_id: 'my-timeline',
css: 'lib/timelinejs/css/timeline.css',
js: 'lib/timelinejs/js/timeline.js'
});
}, 0);
console.log("Running timelineJS");
}
}
});

关于javascript - TimelineJs 和 AngularJs ng-查看内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14473852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com