MUHAMMAD SIDDIQ
MUHAMMAD SIDDIQ

Reputation: 81

JSON array can't be read in AngularJS

I got ng-repeat to read array:

<div layout="row" layout-align="start end">
        <md-input-container flex="20">
            <label>Product Name *</label>
            <md-select required ng-model="review.productName">
                <md-option ng-repeat="pn in productName_array" value="{{pn}}">{{pn}}</md-option>
            </md-select>
        </md-input-container>
    </div>

And this is the angular code,

self.productName_array  = ["abe","abe1","abe2","abe3"];

The resulting array can be read. But it can't be read when I change the initiation to this angular code.

This is the controller:

self.productName_array  = request.getProductName();

This is the request script:

obj.getProductName = function (){
    return $http.get(api_base + 'getProductName').then(function (results) { return results.data;});
};

This is the API script:

private function getProductName(){
    $this->product->findName();
}

This is the query:

public function findName(){
    if($this->get_request_method() != "GET") $this->response('',406);
    $query="SELECT name FROM product p ORDER BY p.id DESC";
    $this->show_response($this->db->get_list_name($query));
}

The result from the angular code is like this:

[["Xiaomi Pocophone F1"],["Samsung Galaxy S9+"],["Samsung Galaxy Note9"],["Iphone XR"],["Iphone XS"],["XIAOMI MI A1 RAM 4\/32GB"],["Iphone XS Max"],["VIVO Y71"],["SAMSUNG GALAXY J2 PRIME"],["SAMSUNG GALAXY J7 PRIME"],["HomeIPHONEIPHONE 6S+"],["HONOR 9 LITE"],["EVERCOSS AT7H"],["Blackberry Aurora"]]

Upvotes: 1

Views: 50

Answers (2)

Tom Edwards
Tom Edwards

Reputation: 124

Although it doesnt really need to be done, if yyou need it as a json use:

var newJson = JSON.stringify(ArrayName);

Upvotes: 1

Bridget Arrington
Bridget Arrington

Reputation: 444

Looking at your dynamic result, it appears that your controller is assigning an array containing an array. To access the values you can use

  <md-option ng-repeat="pn in productName_array[0]" value="{{pn}}">{{pn}}</md-option>

Upvotes: 0

Related Questions