Currently working on a chat application here https://playwithfire.firebaseapp.com/ , and whenever a user adds a new room, I want a new room tab to be introduced. Currently you can add a room, but after that you need to click it to enter this room and display its contents.
I tried to change the md-selected = "selectedIndex" attribute, but it does not activate the tab, so the content does not appear.
Is it possible to do what I ask? What I have so far:
index.html
<div layout="column" ng-controller="RoomController"> <md-tabs md-stretch-tabs md-selected="selectedIndex"> <md-tab ng-repeat="room in roomList" label="{{room.roomName}}"> <div ng-controller="ChatController"> <md-list> <md-item ng-repeat="msg in messages"> <md-item-content> <div class="md-tile-content"> <div class="bubble"> <strong>{{msg.from}}</strong><br> {{msg.body}} </div> </div> </md-item-content> </md-item> </md-list> <div layout="row" layout-margin layout-wrap> <div flex="33"> <label for="nameInput">Username</label> <input ng-model="name" type="text" id="nameInput" placeholder="Enter a username..."> </div> <div flex="95"> <label>Message</label> <textarea class="form-control" ng-model="msg" ng-keydown="addMessage($event)" id="messageInput" placeholder="Type a message..."> </textarea> </div> <div layout="row" layout-sm="column" layout-margin layout-fill layout-align="start end"> <div flex> <md-button class="md-raised md-primary pull-left" ng-click="sendMessage()">Send</md-button> </div> <div flex ng-controller="ModalController"> <md-button class="md-raised md-primary pull-left" ng-click="open()">Add or Join Room</md-button> </div> <div flex ng-controller="HelpController"> <md-button class="pull-right" ng-click="open()" ng-href="">Need help?</md-button> </div> </div> </div> </md-tab> </md-tabs> </div>
room.js
angular.module('myApp') .controller('RoomController', function($scope, ShareFactory) { $scope.roomList = ShareFactory.roomList; // use this to default to index 0 in roomList $scope.selectedIndex = 0; $scope.$on('RoomChange', function(event, data) { $scope.selectedIndex = data; console.log('Heard the change!'); console.log('The data is: ' + data); }); });
modal.js
angular.module('myApp') .controller('ModalController', function($rootScope, $scope, $modal, ChatFactory, ShareFactory) { $scope.open = function () { var modalInstance = $modal.open({ templateUrl: 'views/modal.html', controller: 'ModalInstanceController' }); modalInstance.result.then(function (name) { var found = false; var length = ShareFactory.roomList.length; var index = 0; for(var i = 0; i < length; ++i) { if(ShareFactory.roomList[i].roomName === name) { found = true; index = i; console.log('index ' + index); } } if(!found) { ShareFactory.roomList.push({ roomName : name}); index = ShareFactory.roomList.length - 1; } else {
source share