【问题标题】:Two way data binding don't work in my Angular code两种方式数据绑定在我的 Angular 代码中不起作用
【发布时间】:2016-10-29 20:16:05
【问题描述】:

当我应该看到 $scope.firstname 值时,我只看到一个空白输入框。为什么这不起作用?

index.html:

<!DOCTYPE html>
<html lang="en" ng-app="MyApp" ng-controller="MyController">
<head>
    <meta charset="UTF-8">
    <title>Toto App</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
    <script src="myCtrl.js"></script>
</head>
<body>
<input ng-model="firstname">

</body>
</html>

我的Ctrl.js:

angular.module('MyApp', [])
    .controller('MyController', function($scope) {
        $scope.name = "John";
    });

【问题讨论】:

  • 它在 html 中是“名字”,在控制器中只是“名字”
  • 谢谢我太笨了
  • 对象名称应该相同。您在控制器中使用 $scope.name 但在 html 中使用名字。

标签: javascript angularjs data-binding


【解决方案1】:

您正在使用$scope.name = "John" 而不是$scope.firstname = "John"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 2019-06-27
    • 2015-11-07
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多