簡體   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