繁体   English   中英

AngularJS-范围未在HTML中呈现变量

[英]AngularJS - scope is not rendering the variables in HTML

我有下面的AngularJS代码。

执行以下代码后,不显示当前时间:

<html ng-app="a">

<head>
    <title>Clock App</title>
    <script src="angular.js"></script>
</head>

<body>
    <h1>Clock Application</h1>
    <div ng-controller="t"></div>
    <p> The current time is </p>
    <p ng-bind="timeString"></p>
    <p>{{50+3}}</p>
    </div>
    <script>
        var d = angular.module("a", []);
        d.controller("t", function ($scope) {
            var currentDate = new Date();
            $scope.timeString = currentDate.toTimeString();
        });
    </script>
</body>

</html>

保持控制器为父标签。 然后它将起作用。

更正了以下代码:

 <html ng-app="a"> <head> <title>Clock App</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script> </head> <body ng-controller="t"> <h1>Clock Application</h1> <div></div> <p> The current time is </p> <p ng-bind="timeString"></p> <p>{{50+3}}</p> </div> <script> var d = angular.module("a", []); d.controller("t", function ($scope) { var currentDate = new Date(); $scope.timeString = currentDate.toTimeString(); }); </script> </body> </html> 

由于ng-bind不知道$scope.timeString ,因此timeString值未绑定,因为它不在控制器内部。

尝试这个

<div ng-controller="t">
    <p> The current time is </p>
    <p ng-bind="timeString"></p>
    <p>{{50+3}}</p>
</div>

 <html ng-app="a"> <head> <title>Clock App</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script> </head> <body> <h1>Clock Application</h1> <div ng-controller="t"> <p> The current time is </p> <p ng-bind="timeString"></p> <p>{{50+3}}</p> </div> <script> var d = angular.module("a", []); d.controller("t", function ($scope) { var currentDate = new Date(); $scope.timeString = currentDate.toTimeString(); }); </script> </body> </html> 

暂无
暂无

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

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