I am facing an issue with assigning a value to a variable in my controller when a button is clicked. Here is the code snippet:
var app = angular.module('myApp', ['ngRoute']);
....
var userArray = [
{name: "John", age: 25}, {name: "Jim", age: 30}
];
app.controller('Page2Controller', function($scope) {
$scope.message = 'Look! I am from page 2.';
$scope.newArray = [];
$scope.addUsers = function(){
scope.newArray = userArray;
console.log($scope.newArray);
}
}
app.config(function($routeProvider) {
$routeProvider
.when('/page2', {
templateUrl : 'pages/page2.html',
controller : 'Page2Controller'
...
page2.html
<div>
<p>{{ message }}</p>
<ul>
<li ng-repeat="user in Page2Controller.newArray">{{user.name}}</li>
</ul>
</div>
index.html
<body ng-controller="MainController">
<div id="sideMenu">
<h2>Campaign</h2>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#page2">Page 2</a></li>
<li><a href="#page3">Page 3</a></li>
<li><a href="#page4">Page 4</a></li>
</ul>
</div>
<div ng-view></div>
</body>
I'm confused as to why the $scope.user appears in the console but the "ng-repeat" doesn't display the users on the HTML page. Could it be that ng-repeat can't display data added dynamically?
Any assistance would be greatly appreciated.