Asfandyar Sheikh Asfandyar Sheikh - 1 year ago 58
AngularJS Question

Array being treated as string by angular with loopback backend

I am new to angular and have been trying to get ng-repeat working, but have been unable to do so. The backend is based on Loopback. Here is my code.

<option ng-repeat="item in getCityList" value="{{item}}">{{item}}

is hardcoded as an array, it works e.g.
$scope.getCityList = ["Karachi", "Lahore"]
and each item forms an individual select option, i.e.

However, if I use a function defined on a Loopback model instead, e.g.
self.getCityList = Application.getCityList();
, the two city names are not printed individually, but rather as one select option i.e.
["Karachi", "Lahore"]

It seems that
is outputting the array as string. Is there any way to treat it as an array instead?

Here is the code for

Application.getCityList = function (cb) { var areaList = ["Karachi", "Lahore"]; cb(null, areaList); };

Answer Source

Your problem is the line

self.getCityList = Application.getCityList();

getCityList takes a callback method. It does not return the data, but pass it to the callback.

Application.getCityList(function(err,data) {
    self.getCityList = data;

Should work

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download