Angular Newbie - 无法在控制器函数中使用$ scope变量

时间:2015-03-24 10:27:39

标签: angularjs

我有以下Angular.js控制器代码。

angular.module("myapp", [])
   .controller("JiraReportController", function($scope, $http) {

        $scope.Epics = {};
        $scope.Epics.sprint = "15S04";

        $scope.Epics.getDefectInfo = function(item, event) {
            console.log("Calling getDefectsForEpics.php?sprint=" . $scope.Epics.sprint);
            $http.get("getDefectsForEpics.php?sprint=" . $scope.Epics.sprint)
                .success(function(data, status, headers, config) {
                    $scope.Epics = data;
            })
                .error(function(data, status, headers, config) {
                    alert("AJAX failed!" + config.url);
            }
        );
    }
}

当我访问该函数时,出现以下错误

TypeError: Cannot read property 'Epics' of undefined

我不明白为什么我会收到此错误。请帮助这个Angular Newbie。

4 个答案:

答案 0 :(得分:4)

您的日志中存在语法错误:

console.log("Calling getDefectsForEpics.php?sprint=" . $scope.Epics.sprint);

应该是

console.log("Calling getDefectsForEpics.php?sprint=" + $scope.Epics.sprint);

(将.替换为+$http来电中的错误相同

答案 1 :(得分:1)

在JavaScript中,您使用+连接字符串,而不是.

尝试

console.log("Calling getDefectsForEpics.php?sprint=" + $scope.Epics.sprint);

$http.get("getDefectsForEpics.php?sprint=" + $scope.Epics.sprint)

答案 2 :(得分:0)

首先检查,如上所述,你可以写

console.log("Calling getDefectsForEpics.php?sprint=" . $scope.Epics.sprint);

as

console.log("Calling getDefectsForEpics.php?sprint=", $scope.Epics.sprint);

console.log("Calling getDefectsForEpics.php?sprint="+$scope.Epics.sprint);

我更喜欢通过用逗号分隔它们来传递参数的第一种方式。因为它遵循console.log的签名,如下所示

console.log(obj1 [, obj2, ..., objN);
console.log(msg [, subst1, ..., substN);

对于第9行,将其更改为:

$http.get("getDefectsForEpics.php?sprint=" + $scope.Epics.sprint)

让我们知道你得到的确切错误。

答案 3 :(得分:0)

添加运算符而不是。

console.log("调用getDefectsForEpics.php?sprint =" + $ scope.Epics.sprint);

$ http.get(" getDefectsForEpics.php?sprint =" + $ scope.Epics.sprint