Angular JS- $ scope未定义

时间:2015-06-23 11:15:01

标签: angularjs

  $scope.save = function () {
        $.ajax({
            type: "POST",
            url: "http://localhost:57083/EntityService.asmx/InsertEntity",
            data: "{'Eno':'" + $scope.Eno + "','Ename':'" + $scope.Ename + "','DOB':'" + $scope.DOB + "','State':'" + $scope.State + "','City':'" + $scope.City + "','pin':'" +
            $scope.pin + "','mobile':'" + $scope.mobile + "'}",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (responseData) {
                alert(responseData.d);
            },
            error: function (responseData) {
                alert(responseData.d);
            }

        });
    };

在本代码中,我尝试使用angular JS连接数据库。但它不起作用,它显示错误为" $ scope"未定义。任何人都可以解释一下吗?

2 个答案:

答案 0 :(得分:0)

当模块未被注入控制器的参考区域时,这是一个常见错误,即

myApp.controller('GreetingController', ['$scope', function($scope) {
  ...
}]);

答案 1 :(得分:0)

而不是$ .ajax使用$http服务:

var req = {
 method: 'POST',
 url: 'http://localhost:57083/EntityService.asmx/InsertEntity',
 headers: {
   'Content-Type': "application/json;charset=utf-8"
 },
 data: "{'Eno':'" + $scope.Eno + "','Ename':'" + $scope.Ename + "','DOB':'" + $scope.DOB + "','State':'" + $scope.State + "','City':'" + $scope.City + "','pin':'" +
            $scope.pin + "','mobile':'" + $scope.mobile + "'}",
}

$http(req).success(function(responseData)
{ alert(responseData.d);
})
.error(function(responseData){
 alert(responseData.d);
});

你需要在控制器中注入$ http服务,就像你现在注入$ scope一样。