gpt4 book ai didi

javascript - 如何与 Angular 异步加载脚本

转载 作者:行者123 更新时间:2023-11-28 17:46:35 28 4
gpt4 key购买 nike

由于我在 Angular 和 jQuery 的混合方面有一些不好的经历,所以我试图在 Angular 中避免使用 jQuery。现在,我应该向我的 Angular 项目添加一些代码,但我不确定应该如何转换它。该代码来自其他人,他希望我在项目中实现这一点。

在我的项目中,我已将所有必需的库添加到index.html 中。我认为这是唯一可以添加库的地方。

有人知道(如果有办法的话)如何将这些 jquery 代码行转换为 Angular 或 javascript 代码吗?我应该在项目的哪个位置添加这个?

$('head').append( '<meta name="PAGENAME" content="$pagename">' );

$(document).ready(function() {

$.getScript( "https://www.website.com/crsc/scripts/script.js" )
.done(function( script, textStatus ) {
console.log( 'loaded test' );
})

.fail(function( jqxhr, settings, exception ) {
console.log('not loaded');
});

});

最佳答案

您可以使用app.run,它将在应用加载所有内容之前像main方法一样运行

     app.run(["scriptService"],function(scriptService){
scriptService.GetScripts()
.then(function(success){console.log("loaded test");},
function(error){ console.log("not loaded");}
}]);

接下来您所要做的就是创建一个脚本服务。

    app.factory("scriptService", ["$http", function ($http) {
this.GetScripts = function ()
{
return $http.get(""https://www.website.com/crsc/scripts/script.js") .then(function(result) { return result});

}

但实际上为什么不直接通过脚本标签导入脚本?异步加载脚本,因为 op 不知道这是一个选项,我想我会将它包含在这里 ww3 schools async loading

  <script src="demo_async.js" async></script>

definition and Usage The async attribute is a boolean attribute.

When present, it specifies that the script will be executed asynchronously as soon as it is available.

Note: The async attribute is only for external scripts (and should only be used if the src attribute is present).

Note: There are several ways an external script can be executed:

If async is present: The script is executed asynchronously with the rest of the page (the script will be executed while the page continues the parsing) If async is not present and defer is present: The script is executed when the page has finished parsing If neither async or defer is present: The script is fetched and executed immediately, before the browser continues parsing the page Browser Support The numbers in the table specify the first browser version that fully supports the attribute.

关于javascript - 如何与 Angular 异步加载脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46586904/

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