I've got a file called test.js
with the following code:
function test(str) {
return 'Hello, ' + str + '!'
}
My goal is to utilize the test method in my Angular controller:
angular.module('testModule')
.controller('testController', ['$scope', function($scope){
console.log(test('John'))
}
This will result in Hello, John!
I attempted the following:
<div ng-app="testModule" ng-controller="testController">
<script type="text/javascript">
function test(str) {
return 'Hello, ' + str + '!'
}
</script>
</div>
It worked as expected, returning Hello, John!
. However, trying to reference the method from my other .js
files leads to
ReferenceError: ____ is not defined
.
- How can I access methods from other
.js
files within my Angular controller? - What is the recommended approach for invoking these methods? (e.g. Do I need to migrate the code from all my
.js
files into Angular's model or controller?)