Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Aditya Khanna
PLUS
Aditya Khanna
Courses Plus Student 200 Points

ngoptions erratic behaviour

I am developing an expressjs based app and am using angular with it, just to clarify i am an angular newbie and the problem i am facing is quite peculiar, i am fetching data from an external resource using $http and then using ngoptions to populate a select element, code is as below.

<html ng-app="lsplapp"> <head> <script type='text/javascript' src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js"></script> <script type="text/javascript"> var app = angular.module('lsplapp', []); app.controller('Test', function($scope, $http){ $scope.selectedItem = null; var http_obj = $http.post("http://localhost:3000/api/",{mfunc:"gt"}) http_obj.success(function(data, status, headers, config){ $scope.items = data['200']; $scope.selectedItem = $scope.items[0]; }) http_obj.error(function(data, status, headers, config){ console.log(data) }) console.log($scope.selectedItem) // $scope.items = [{name: 'one', age: 30 },{ name: 'two', age: 27 },{ name: 'three', age: 50 }]; // $scope.selectedItem = $scope.items[0]; }); </script> </head> <body> <div ng-controller="Test"> <p>selected item is : {{selectedItem}}</p> <p> age of selected item is : {{selectedItem.staff_name}} </p> <select ng-model="selectedItem" ng-options="item.staff_name for item in items track by item._id"> </select> </div> </body> </html>

this page works fine when i run it individually from my local disk , the selectedItem gets updated and i get to see the updated selectedItem values as the selection in the combo changes. but as soon as i attach this page as a view to a route in my expressjs app , the combo gets populated with the relevant data , but the selectedItem model does not get populated , i.e. the value of the selectedItem model does not get updated and defaults to null.

Any Pointers will be great, TIA

Sorry for the late response. Do you still need help with this?

Let me know.