1

the variable comes empty when binded on html

HTML code:

<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 code:

 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");
        });
    };


});

The console.log() shows the info that i want but for some reason it doesnt go to the html Also the rooms[0].name works just the room[0].name doesnt work

3
  • what is response.data? An array? Can you log what response.data is? Commented Mar 4, 2016 at 1:02
  • How is your getRoom called? Commented Mar 4, 2016 at 1:10
  • its called with ng-click ng-click="getRoom( r.id )" Commented Mar 4, 2016 at 1:26

2 Answers 2

1

Looks like you call $scope.getRoom outside of Angular context. You either need to use Angular bindings like ng-click, or manually do $scope.$apply.

Sign up to request clarification or add additional context in comments.

1 Comment

it was out of the controller scope
0

At the beginning $scope.rooom[0] is undefined so angular crash when you want room[0].name. try this {{ room[0] ? room[0].name: '' }}

1 Comment

No, for Angular bindings it doesn't matter

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.