0

我希望你能帮助我。我对 Angular 很陌生,所以我可能会犯一些愚蠢的错误,但是,当给 ng-app 赋值时,AngularJs 不起作用。我在这里举一个例子:这是我的home.html不起作用(当我说“不起作用”时,我的意思是我看到打印的“{{name}}”而不是它的值)。

<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
<script>
var req={
        method: 'POST',
        url: 'http://localhost:8080/ConnectionDb/WebAppDbServlet',
        data: {"nome":$scope.name}
    }
    var app=angular.module('noteApp', [])
    app.controller('noteCtrl', function ($scope, $http){
        $scope.addNote = function () {
            $http(req).success(function(data, status, headers, config) {
                $scope.nome = data;
            }).error(function(data, status, headers, config) {

            });
        }
    })
</script>
</head>
<body ng-app="noteApp">
    <div ng-controller="noteCtrl">
        <form>
            <div>
            Insert your name: <input type="text" name="name" data-ng-model="name"><br>
            </div>
        </form>
        <p>Hola {{name}}</p>
    </div>
</body>
</html>

但如果我像这样改变它

<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
<script>
var req={
        method: 'POST',
        url: 'http://localhost:8080/ConnectionDb/WebAppDbServlet',
        data: {"nome":$scope.name}
    }
    var app=angular.module('noteApp', [])
    app.controller('noteCtrl', function ($scope, $http){
        $scope.addNote = function () {
            $http(req).success(function(data, status, headers, config) {
                $scope.nome = data;
            }).error(function(data, status, headers, config) {

            });
        }
    })
</script>
</head>
<body ng-app>
    <div>
        <form>
            <div>
            Insert your name: <input type="text" name="name" data-ng-model="name"><br>
            </div>
        </form>
        <p>Hola {{name}}</p>
    </div>
</body>
</html>

它应该完美无缺。有什么建议吗?

4

3 回答 3

0

您正在req使用设置变量,$scope.name但此时未定义范围。如果你打开控制台,你会看到一个关于的错误。尝试这个:

http://plnkr.co/edit/LqZNX8BYnCnbUD29YRfi?p=preview

<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
<script>
var req={
        method: 'POST',
        url: 'http://localhost:8080/ConnectionDb/WebAppDbServlet',
    }
    var app=angular.module('noteApp', [])
    app.controller('noteCtrl', function ($scope, $http){
        $scope.addNote = function () {
            req.data={"nome":$scope.name}; 
            $http(req).success(function(data, status, headers, config) {
                $scope.name = data;
            }).error(function(data, status, headers, config) {

            });
        }
    })
</script>
</head>
<body ng-app="noteApp">
    <div ng-controller="noteCtrl">
        <form>
            <div>
            Insert your name: <input type="text" name="name" data-ng-model="name"><br>
            </div>
        </form>
        <p>Hola {{name}}</p>
    </div>
</body>
</html>

错误的变量名称(“nome”而不是“name”)与您的问题无关,但仍需要更正

于 2016-02-09T09:45:42.917 回答
0

在您的视图中,您正在使用{{ name }}但在控制器内部,您将数据放入$scope.nome.

只是改变$scope.nome > $scope.name

于 2016-02-09T09:45:52.887 回答
0

您在“var app=angular.module('noteApp', [])”之后缺少一个“;”,因此 noteApp 没有被初始化。

于 2016-10-04T06:37:22.093 回答