无法将范围变量绑定到 angular js 中的 html 元素
Unable to bind the scope variable to the html element in angular js
我从函数 $scope.getPIRData
中得到了一些值,例如 "OPEN" 或 "CLOSE",我存储在 $scope.pirstatus
中,我试图在HTML Header 标签<h1> {{pirstatus}}</h1>
但它在使用 javascript document.getelementbyid
.
时在按钮点击中没有显示任何内容
<!DOCTYPE html>
<html ng-app="PIR_Detection">
<head>
<meta charset="utf-8" />
<title>PIR Door Monitoring</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
</head>
<body ng-controller="myCtrl">
<div class="col-sm-4 col-lg-offset-4" ng-controller="myCtrl">
<select class="form-control" ng-model="sel_val" ng-change="getPIRData(sel_val.deveui)" ng-options="data.deveui for data in Customers">Select PIR Device</select>
</div>
<br />
<span style="font-size:14px" id="pirstatus"></span>
<h1> {{pirstatus}}</h1>
<span ng-bind="pirstatus"></span>
<script>
var app = angular.module('PIR_Detection', []);
app.controller('myCtrl', function ($scope, $http, $window) {
$scope.sel_val = 0;
$scope.DefaultLabel = "Loading.....";
var post = $http({
method: "get",
url: "../data.json",
dataType: 'json',
data: {},
headers: { "Content-Type": "application/json" }
});
post.success(function (data, status) {
$scope.Customers = data;
});
post.error(function (data, status) {
});
$scope.getPIRData = function (id) {
var url = "/PIRDetails/GetPIRStatus/" + id;
$http.get(url)
.then(function (response) {
$scope.myWelcome = response.data;
$scope.pirstatus = base64toHEX($scope.myWelcome.dataFrame);
document.getElementById("pirstatus").innerHTML = $scope.pirstatus;
});
};
});
</script>
</body>
</html>
您可以尝试在 AngularJS 中使用 ng-bind
属性。
在你的代码中,如果你修改 h1
标签,下面应该可以工作
<h1 ng-bind="pirstatus"></h1>
请参考这个例子 - https://www.w3schools.com/angular/tryit.asp?filename=try_ng_ng-bind
我从函数 $scope.getPIRData
中得到了一些值,例如 "OPEN" 或 "CLOSE",我存储在 $scope.pirstatus
中,我试图在HTML Header 标签<h1> {{pirstatus}}</h1>
但它在使用 javascript document.getelementbyid
.
<!DOCTYPE html>
<html ng-app="PIR_Detection">
<head>
<meta charset="utf-8" />
<title>PIR Door Monitoring</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
</head>
<body ng-controller="myCtrl">
<div class="col-sm-4 col-lg-offset-4" ng-controller="myCtrl">
<select class="form-control" ng-model="sel_val" ng-change="getPIRData(sel_val.deveui)" ng-options="data.deveui for data in Customers">Select PIR Device</select>
</div>
<br />
<span style="font-size:14px" id="pirstatus"></span>
<h1> {{pirstatus}}</h1>
<span ng-bind="pirstatus"></span>
<script>
var app = angular.module('PIR_Detection', []);
app.controller('myCtrl', function ($scope, $http, $window) {
$scope.sel_val = 0;
$scope.DefaultLabel = "Loading.....";
var post = $http({
method: "get",
url: "../data.json",
dataType: 'json',
data: {},
headers: { "Content-Type": "application/json" }
});
post.success(function (data, status) {
$scope.Customers = data;
});
post.error(function (data, status) {
});
$scope.getPIRData = function (id) {
var url = "/PIRDetails/GetPIRStatus/" + id;
$http.get(url)
.then(function (response) {
$scope.myWelcome = response.data;
$scope.pirstatus = base64toHEX($scope.myWelcome.dataFrame);
document.getElementById("pirstatus").innerHTML = $scope.pirstatus;
});
};
});
</script>
</body>
</html>
您可以尝试在 AngularJS 中使用 ng-bind
属性。
在你的代码中,如果你修改 h1
标签,下面应该可以工作
<h1 ng-bind="pirstatus"></h1>
请参考这个例子 - https://www.w3schools.com/angular/tryit.asp?filename=try_ng_ng-bind