在我的HTML页面中,我有这个功能:
<button ng-controller="getURL" ng-href="{{getLoginUrl}}">Login</button>
<script src="js/app.js"></script>
<script type="text/javascript">
function getURL($scope) {
$scope.getLoginUrl = '/my-url';
}
</script>
在我的 js/app.js 中,我的 Controller 具有在内联 HTML 中定义的函数 getURL()
:
'use strict';
angular.module('myApp', [])
.controller('getURL',['$scope',getURL])
但我收到此错误:
Uncaught ReferenceError: getURL is not defined
我在 getURL Controller 之后放置的所有 Controller 都不起作用,并且出现错误Argument 'anotherController' is not a function, got undefined
。
最佳答案
将函数声明移到 app.js
之前,因为您尝试在声明之前使用函数
<script type="text/javascript">
function getURL($scope) {
$scope.getLoginUrl = '/my-url';
}
</script>
<script src="js/app.js"></script>
关于javascript - 如何内联定义 AngularJS 函数(在 HTML 中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33709467/