Search code examples
javascriptangularjsangularjs-scopeangularjs-select

unable to push string to listbox in angularjs


When I'm trying to push a string(New) to a list box it was inserting as "undefined" to it.

  $http({
            method: 'GET',
            url: 'http://xxx/api/Maintenance/GetAllFilteredItems',
            params: { Pt_Id: PtId}
        }).then(function successCallback(response) {
            $scope.items = response.data;         
        }, function errorCallback(response) {
            // alert(response);
        });



 $scope.AddNew = function () {
     var Item = [];
        Item[0] = 'New'; alert(Item.length); 
      //  $scope.items.splice(0, 0, Item[0].toString());
        $scope.items.splice(0, 0, Item[0]);
        //  $scope.items.push($scope.input);
        //  $scope.items.splice(0, 0, { itm: 'New'});
        $scope.itm = $scope.items[0];     


        //var item = new String('New')
        //$scope.items.splice(0, 0, item);
        //$scope.items.unshift(item);
}

Tried in different ways as above but no luck.


Solution

  • Hope the items were in $scope.items are objects and you're trying to insert a string to that listbox so it was inserting as undefined.

    Try to push that items like below to listbox then you can push any string which you want.

    public List yourmethod() {....} // to $scope.items