Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在服务器上运行javascript时未定义_Javascript_Angularjs - Fatal编程技术网

在服务器上运行javascript时未定义

在服务器上运行javascript时未定义,javascript,angularjs,Javascript,Angularjs,这让我无法理解以下代码: <!DOCTYPE html> <html ng-app> <head> <script src="http://code.jquery.com/jquery-1.8.0.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>

这让我无法理解以下代码:

<!DOCTYPE html>
<html ng-app>
<head>
<script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
<script>
function ctrl($scope){
    $scope.submit = function(e){
        var elem = angular.element(e.srcElement);
        alert($(elem.parent()).html());
        //alert ('first name ' + person.first + ' last name ' + person.last);
    };
}
</script>
</head>
<body>
<div ng-controller="ctrl">
First: <input name="first" value="will" />
Last: <input name="last" value = "kriski" />
    <input type="submit" name="submit" ng-click="submit($event)"/>
</div>
</body>
</html>

函数ctrl($scope){
$scope.submit=函数(e){
var elem=角度元素(e.src元素);
警报($(elem.parent()).html());
//警报('first name'+person.first+'last name'+person.last);
};
}
第一:
最后:
在jsfiddle和plunker中工作很好(单击Submit,但在警报中显示HTML)

但当我在本地和我的个人服务器上运行相同的代码时,警报显示为“未定义”

这两种方法都有效:

在我的个人服务器上粘贴的相同代码不存在

非常困惑

请尝试使用此代码

<script type = "text/javascript">
    $(document).ready(function(e) {
        function ctrl($scope)
        {
            $scope.submit = function(e){
                var elem = angular.element(e.srcElement);
                alert($(elem.parent()).html());
                //alert ('first name ' + person.first + ' last name ' + person.last);
            };
        }
    });
</script>

$(文档).ready(函数(e){
函数ctrl($scope)
{
$scope.submit=函数(e){
var elem=角度元素(e.src元素);
警报($(elem.parent()).html());
//警报('first name'+person.first+'last name'+person.last);
};
}
});

或者在关闭

之前写
函数ctrl($scope){//Your code goes here}
Doh实际上我再次意识到错误只发生在Firefox中,而不是Chrome中,并且与与与Firefox不兼容的src元素有关。这篇文章解释了一个修复方法

将您的函数包含在$(document)中。ready()检查onceNope查看下面的注释,正如您从Plunker&JSFIDLE中看到的,它在没有匿名函数的情况下工作不应用匿名函数是不必要的,一个是加载DOM后click事件运行,另一个是它将停止Angular代码运行