James D. James D. - 3 years ago 168
AngularJS Question

UI-SELECT binding to controller.js in Array?

First of all, thank you for your reading.
I am new to the language.

I am trying to use UI-SELECT and bind the data to controller.js to save into database.

I know that UI-SELECT should be used with ng-model in "A.selected" way.

I searched for some help but couldn't find how to make the below code in array style to work.

[HTML with UI-Select]

<ui-select ng-model = "project_customer_company.selected" theme="bootstrap">
<ui-select-match placeholder="">
{{$select.selected.customer_company_name}}</ui-select-match>
<ui-select-choices repeat="customer in customers | filter: $select.search">
<div ng-bind-html="customer.customer_company_name | highlight: $select.search"></div>
<div ng-bind-html="customer.customer_company_ceo | highlight: $select.search"></div>
</ui-select-choices>




[Controller.js]

$scope.project_customer_company = {};

$http.post("../crud/projects_insert.php", {
project_title : $scope.project_title,
project_customer_company : $scope.project_customer_company // coming from <UI-SELECT>, and this has to be changed like in array style but how?
})


I think I am almost there, but it is returning only "array" sign.

I know this might be easy, but I am lost for almost a whole day.

Any advice would be highly appreciated and hope this issue helps the others as well. :)

Thank you and have a good day!

Answer Source

ui-select will return the ng-model as an object only not an array. You can use the . syntax as shown in the below example and access the variables needed.

  $scope.fetch = function() {
    alert($scope.project_customer_company.selected.customer_company_name);
    alert($scope.project_customer_company.selected.customer_company_ceo);
  }

In the above code, you can see that first we access the selected property of the object $scope.project_customer_company, then using another . we access the individual properties customer_company_name and customer_company_ceo

JSFiddle Demo

Let me know if you face any issues!

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