Asfandyar Sheikh Asfandyar Sheikh - 4 months ago 11
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.

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


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


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
Application.getCityList()
is outputting the array as string. Is there any way to treat it as an array instead?

Here is the code for
Application.getCityList()


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

Answer

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