{{}}运算符不在angularjs中工作

时间:2017-12-16 03:21:05

标签: javascript angularjs

当我尝试打印变量名称时,它会打印 {{name}} 而不是变量,我不知道我哪里出错了。

HTML:

<!DOCTYPE html>
<html ng-app="MyTestApp">
<head>
    <title>Angular test</title>
    <script type="text/javascript"> src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <script type="text/javascript"> src = "app.js"></script>
</head>
<body>
    <div id="divx" ng-controller="MyTestController">
        <input type="text" ng-model="name">
                <h2>{{name}}</h2>
    </div>  

</body>
</html>

JavaScript的:

(function () {
'use strict';

angular.module('MyTestApp', [])

.controller('MyTestController', function ($scope) {
  $scope.name = "Angular";
  $scope.sayHello = function () {
    return "From function";
  };
});

})();

1 个答案:

答案 0 :(得分:5)

有两个问题,

(i)脚本应该被称为

  <script type="text/javascript" src="angular.min.js"> </script>
  <script type="text/javascript" src="app.js"> </script>

(ii)您不应在输入元素中放置 {{name}} 。在 <h1> <p> 标记内进行更改,如下所示。

<强>样本

(function () {
'use strict';

angular.module('MyTestApp', [])

.controller('MyTestController', function ($scope) {
  $scope.name = "Angular";
  $scope.sayHello = function () {
    return "From function";
  };
});

})();
<!DOCTYPE html>
<html ng-app="MyTestApp">
<head>
    <title>Angular test</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>
<body>
    <div id="divx" ng-controller="MyTestController">
        <input type="text" ng-model="name">
        <h1> {{name}}</h1>
    </div>  

</body>
</html>