简体   繁体   English

如何从外部js文件导入和使用方法到AngularJS中?

[英]How import and use method from outer js-file into AngularJS?

I should like to use functional from this file https://ahunter.ru/js/min/ahunter_suggest.js 我想使用此文件中的功能https://ahunter.ru/js/min/ahunter_suggest.js

The documentation says that 1. import files 文档说1.导入文件

<head>
...
  <script src="path_to_scripts/jquery.min.js"></script>
  <script src="path_to_scripts/ahunter_suggest.js"></script>
...
</head>
  1. Add id for input field 为输入字段添加ID
<div>
  <input id="js-AddressField" placeholder="Введите адрес">  
</div>
  1. Add script 添加脚本
<script>
  var options = { id : 'js-AddressField' };
  AhunterSuggest.Address.Solid( options );
</script>

I added file to my project directory and added link into index.html, but i can't call AhunterSuggest.Address.Solid( options ); 我将文件添加到项目目录中,并在index.html中添加了链接,但无法调用AhunterSuggest.Address.Solid( options ); in one of my controller. 在我的控制器之一中。

How will do it right? 怎么做对?

In your AngularJS controller, u have a method for initialising the controller right? 在您的AngularJS控制器中,您有一种初始化控制器的方法吗? If so, you have to call 3rd party library initialiser (3rd code in your message) 如果是这样,您必须调用第三方库初始化程序(消息中的第三代码)

Make sure to call this, after dom is ready and angularJS controller initiated. 确保在dom准备好并启动angularJS控制器后调用此函数。

Your angular controller before (atm) 您之前的角度控制器(atm)

init() {
  this.xxx()
}

Angular Controller after: (should be sth like this) Angular Controller之后:(应该是这样)

init() {
  this.xxx()
  this.initialiseAHunterSuggest()
}
initialiseAHunterSuggest() {
  var options = { id : 'js-AddressField' }
  AhunterSuggest.Address.Solid( options )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM