我有test.js
其中包含:如何從Angular的JS文件中調用方法?
function test(str) {
return 'Hello, ' + str + '!'
}
我想使用的測試方法在我的角度控制器:
angular.module('testModule')
.controller('testController', ['$scope', function($scope){
console.log(test('John'))
}
它返回Hello, John!
我曾嘗試:
<div ng-app="testModule" ng-controller="testController">
<script type="text/javascript">
function test(str) {
return 'Hello, ' + str + '!'
}
</script>
</div>
其中工作作爲預計,返回Hello, John!
。但試圖從我的其他.js
文件中引用該方法返回ReferenceError: ____ is not defined
。
- 如何在我的Angular控制器中調用其他
.js
文件的方法? - 調用這些方法的最佳做法是什麼? (例如,我一定要在我所有的
.js
文件傳輸的代碼轉換成角的模型或控制器?)
爲該指令做出指示 –
用Angular方式創建一個[service](https://docs.angularjs.org/guide/services)。 'Angular服務是: **懶惰實例化** - Angular只在應用程序組件依賴它時實例化一個服務。 **單身** ** - 依賴於服務的每個組件獲取對服務工廠生成的單個實例的引用。' –