简体   繁体   English

Angular JS- $ scope未定义

[英]Angular JS-$scope is undefined

  $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);
            }

        });
    };

In this Code i am trying to connect with database using angular JS. 在此代码中,我尝试使用angular JS与数据库连接。 But it is not working,it shows error as "$scope" is undefined.Can anyone explain? 但是它不起作用,因为“ $ scope”未定义,它显示错误。任何人都可以解释吗?

This is a common error when a module has not been injected in the reference area of the controller ie 当尚未将模块注入控制器的参考区域中时,这是一个常见错误,即

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

Instead of $.ajax use $http service: 代替$ .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);
});

you need to inject $http service in controller the same way as you have injected $scope now. 您需要像现在注入$ scope一样将$ http服务注入控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM