绑定(bind)到 html 时变量为空
HTML 代码:
<html lang="en" ng-app="roomInfo">
...
<div id="modal" ng-controller="roomListCtrl" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">{{ room[0].name }}
JS代码:
var roomInfo = angular.module('roomInfo', []);
roomInfo.controller('roomListCtrl', function ($scope, $http) {
$http.get("/room-list-info/").then(function(response) {
$scope.rooms = response.data;
});
$scope.getRoom = function (id) {
var url = "/room-info/"+ id +"/";
console.log(id);
$http.get(url).then(function(response) {
$scope.room = response.data;
console.log($scope.room[0].name + "hola");
});
};
});
console.log() 显示了我想要的信息,但由于某种原因它没有转到 html 此外, rooms[0].name 有效,而 room[0].name 无效
最佳答案
看起来您在 Angular 上下文之外调用了 $scope.getRoom。您要么需要使用 Angular 绑定(bind)(例如 ng-click),要么手动执行 $scope.$apply。
关于javascript - ng-binding,在 html 中调用时 $scope 变量为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35786120/