1

I'm using Angular, a bootstrap admin web app to create my web application, the problem I'm facing is how to read values from angular element.

This is how the element looks like.

<ui-select  ng-model="selected" theme="bootstrap" ng-change='test()'>
<ui-select-match  placeholder="Select by typing client's name or number">{{$select.selected.first_name}}</ui-select-match>
<ui-select-choices ng-value='mikeee' group-by="'group'" repeat="id_number in clients_list | filter: $select.search">
          <span ng-bind-html="id_number.first_name + ' - ' | highlight: $select.search"></span>
          <small ng-bind-html="id_number.registration_number | highlight: $select.search"></small>
        </ui-select-choices>
      </ui-select>

And the test() function:

$scope.test = function()  
{
    var selectedValue = $.param({
        client_number : $scope.selected,
    });

    alert(selectedValue)
}

When the value in the element changes, I get an alert box but the Client_number is empty. Thank you.

2
  • Are you trying to serialize data? Commented Oct 31, 2016 at 7:24
  • @Nikhil, no just the value when a user makes a selection. Commented Oct 31, 2016 at 7:49

1 Answer 1

1

angular
  .module('myApp', [])
  .run(function($rootScope) {
    $rootScope.title = 'myTest Page';
  })
  .controller('testController', ['$scope',
    function($scope) {

      $scope.test = function() {
        var selectedValue = serializeData({
          client_number: $scope.selected
        });

        alert(selectedValue)
      }



      function serializeData(data) {
        // If this is not an object, defer to native stringification.
        if (!angular.isObject(data)) {
          return ((data == null) ? "" : data.toString());
        }

        var buffer = [];

        // Serialize each key in the object.
        for (var name in data) {
          if (!data.hasOwnProperty(name)) {
            continue;
          }

          var value = data[name];

          buffer.push(
            encodeURIComponent(name) + "=" + encodeURIComponent((value == null) ? "" : value)
          );
        }

        // Serialize the buffer and clean it up for transportation.
        var source = buffer.join("&").replace(/%20/g, "+");
        return (source);
      }
    }
  ])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<input ng-app="myApp" ng-controller="testController" ng-model="selected" theme="bootstrap" ng-change='test()' />

Sign up to request clarification or add additional context in comments.

1 Comment

For normal text input and select box, it works well and I can get the value. The problem comes in when I try to do the same on a custom controller element. <ui-select>...</ui-select>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.