gpt4 book ai didi

javascript - 使用 grunt 在 Angular 指令中调用 js 文件

转载 作者:行者123 更新时间:2023-12-03 10:44:24 24 4
gpt4 key购买 nike

在使用grunt build命令之前,我编写了一个非常有效的指令。事实上,我下面的初始化不再起作用,因为构建后库的路径不正确:

element.intlTelInput({
validationScript: "bower_components/intl-tel-input/lib/libphonenumber/build/isValidNumber.js",
preferredCountries: ['ch', 'fr']
});

是否有可能有一个在应用程序和dist(在grunt构建之后)上下文或其他类似的上下文中都有效的相对路径?

感谢您的帮助

最佳答案

您可以尝试 grunt-ng-constant,有两种配置,一种用于本地环境,另一种用于生产环境,并在构建任务中在所有进程之前添加生产 ngconstant,例如:

ngconstant: {
options: {
dest: 'app/scripts/configuration.js',
name: 'configuration'
},
local: {
constants: {
'validationScript': 'bower_components/intl-tel-input/lib/libphonenumber/build/isValidNumber.js',
}
},
dist:{
constants: {
'validationScript': 'prodPathFile',
}
}
},


grunt.registerTask('build', [
'ngconstant:dist',
'other_tasks'
]);

只需要为您的 Angular 应用程序配置模块,并在指令中使用常量validationScript,例如:

app.directive('yourdirective', ['validationScript', function(validationScript) {
return {
link: function(scope, element) {
element.intlTelInput({
validationScript: validationScript,
preferredCountries: ['ch', 'fr']
});
}
}
}]);

关于javascript - 使用 grunt 在 Angular 指令中调用 js 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28625740/

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