【问题标题】:Angularjs TypeError: undefined is not a functionAngularjs TypeError:未定义不是函数
【发布时间】:2012-10-23 17:28:04
【问题描述】:

我刚刚开始使用 Angular js,我正在尝试通过发布操作加载数据。

我正在使用 angularjs v.1.0.2

这是我的代码: HTML:

<!DOCTYPE html>
<html ng-app>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <title></title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width">

        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js"></script>
        <script src="<?php echo $baseUrl?>/js/profilling/main.js"></script>
    </head>
    <body>
        <div ng-controller="GroupsCtrl">

        </div>
    </body>
</html>

main.js:

function GroupsCtrl($scope, $http) {
    $scope.url = '/file.php';
    $scope.images = [];

    function handleGroupsLoaded(data, status) {
        console.log(data);
    }

    $scope.fetch = function () {
        $http.post($scope.url).success($scope.handleGroupsLoaded);
    }

    $scope.fetch();
}

我正在尝试关注这个 jsfiddle:http://jsfiddle.net/simpulton/wHL3F/

但我收到以下错误:

TypeError: undefined is not a function 在http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js:92:92 在我 (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js:76:119) 在http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js:76:352 在 Object.e.$eval (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js:86:220) 在 Object.e.$digest (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js:84:198) 在 Object.e.$apply (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js:86:379) 在 e (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js:92:330) 在 o (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js:95:407) 在 XMLHttpRequest.q.onreadystatechange (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js:96:334)

谁能帮忙?

谢谢

编辑 文件.php:

echo '{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}
';

这似乎是一个有效的 json 对象。

【问题讨论】:

  • 也许你只需要将function handleGroupsLoaded(data, status)声明为$scope.handleGroupsLoaded = function(data, status) {...}
  • 现在我觉得自己很笨 :) 谢谢!!像魅力一样工作!

标签: javascript html angularjs javascriptmvc


【解决方案1】:

您只需在$scope注册handleGroupsLoaded()

function GroupsCtrl($scope, $http) {
    $scope.url = '/file.php';
    $scope.images = [];

    $scope.handleGroupsLoaded = function(data, status) {
        console.log(data);
    }

    $scope.fetch = function () {
        $http.post($scope.url).success($scope.handleGroupsLoaded);
    }

    $scope.fetch();
}

【讨论】:

    猜你喜欢
    • 2015-03-13
    • 2016-01-14
    • 2015-08-04
    • 2014-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多