javascript - AngularJS resetting array of arrays -
i asked question similar , tried solution worked no luck. pretty simple problem. have following :
<script type="text/javascript"> 'use strict'; var app = angular.module('app', ['appcontrollers']); "use strict"; var appcontrollers = angular.module('app', []); appcontrollers.controller('personcontroller', ['$scope', '$http', function ($scope, $http) { $scope.responses = [[], [], []]; $scope.addcoach = function() { $scope.responses[0].push($scope.coach.name); }; $scope.addathlete = function() { $scope.responses[1].push($scope.athlete.name); }; $scope.addsupportstaff = function() { $scope.responses[2].push($scope.employee.name); };
so i'm trying add list of coaches, athletes , supporting staff respective arrays within array when check console log of $scope.responses shows added one.
edit:
so i've changed code following:
<script type="text/javascript"> 'use strict'; var app = angular.module('app', ['appcontrollers']); "use strict"; var appcontrollers = angular.module('app', []); appcontrollers.controller('personcontroller', ['$scope', '$http', function ($scope, $http) { $scope.responses = { coaches: [], athletes: [], employees: [] }; $scope.addcoach = function() { $scope.responses.coaches.push($scope.coach.name); console.log($scope.responses); $scope.coach = {}; // clean form fields }; $scope.addathlete = function() { $scope.responses.athletes.push($scope.athlete.name); console.log($scope.responses); $scope.athlete = {}; // clean form fields }; $scope.addsupportstaff = function() { $scope.responses.employees.push($scope.employee.name); console.log($scope.responses); $scope.employee = {}; // clean form fields };
}]);
html
<h1>coaches attending</h1> <div data-ng-app="app" data-ng-controller="personcontroller"> <div> <div> <input type="text" class="span3" placeholder="full name" ng-model="coach.name"> <button type="button" ng-click="addcoach()"> add </button> </div> <div > <ul><li ng-repeat="coach in responses.coaches">{{coach}}</li></ul> </div> </div> <br> <h1>athletes attending</h1> <div data-ng-app="app" data-ng-controller="personcontroller"> <div> <div> <input type="text" class="span3" placeholder="full name" ng-model="athlete.name"> <button type="button" ng-click="addathlete()"> add </button> </div> <div > <ul><li ng-repeat="athlete in responses.athletes">{{athlete}}</li></ul> </div> </div> <br> <h1>support staff attending</h1> <div data-ng-app="app" data-ng-controller="personcontroller"> <div> <div> <input type="text" class="span3" placeholder="full name" ng-model="employee.name"> <button type="button" ng-click="addsupportstaff()"> add </button> </div> <div > <ul><li ng-repeat="employee in responses.employees">{{employee}}</li></ul> </div> </div>
but problem when add in coach column after adding in athlete or support staff, removes previous entries.
fixed it. defining controller , app multiple time giving multiple instances.
Comments
Post a Comment