javascript - 如何内联定义 AngularJS 函数(在 HTML 中)?

标签 javascript angularjs

在我的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/

相关文章:

javascript - Onload 事件不适用于 Internet Explorer

javascript - 如何将数据推送到json文件中?

javascript - CSS `transform 的 JQuery UI `Slide` 转换问题

javascript - 如何清除 jQuery 中的文本区域值?

javascript - 从 .aspx.cs 页面访问一个变量值(在循环中)到 javascript

javascript - XUL 和 JavaScript

javascript - 如何永久更改 CSS 文件,以便将更改保存在服务器上的实际文件中?

angularjs - Angular-UI-Bootstrap 自定义工具提示/带有 2 路数据绑定(bind)的弹出框

javascript - 使用 toISOString() 获取 ng-repeat 中的纪元日期

angularjs - 使用 Socket IO、Laravel、Redis、Angularjs 向特定用户发送数据