0

I can seem to figure this one out even with the help of other posts. It is only happening in chrome. The error is coming at

angular.extend($scope.selectedCompany, $scope.companyId);

$scope.companies = {};
$scope.companies = Company.query(function () { });
$scope.selectedCompany = $sessionStorage.$default($scope.companies[1]);
$scope.selectCompany = function () {
    $rootScope.active3 = $scope.companyId;
    angular.extend($scope.selectedCompany, $scope.companyId);

    var id = $rootScope.active3
    $http.get('/api/apiCompany/' + id)
        .success(function (result) {
            $scope.CompanyName = result.CompanyName
            console.log($scope.CompanyName);
        });
};//

View

<label>Company Name</label>
<select class="form-control" ng-model="companyId" ng-change="selectCompany()"
    ng-options="company.CompanyId as company.CompanyName for company in companies">
    {{company.CompanyName}}
</select>

Here is what the return json looks like

[{"$id":"1","CompanyId":1,"CompanyName":"Black_Elk","Documents":null},{"$id":"2","CompanyId":2,"CompanyName":"Saratoga","Documents":null},{"$id":"3","CompanyId":3,"CompanyName":"Three_Rivers","Documents":null},{"$id":"4","CompanyId":4,"CompanyName":"Transparent_Energy","Documents":null}]

2
  • Could you provide more information on what's in $scope.selectedCompany and $scope.companyId ? Its most likely that one of these arent objects... Commented Feb 16, 2015 at 19:55
  • i posted the view. let me know what else you need. thanks Commented Feb 16, 2015 at 20:02

1 Answer 1

1

angular.extend expects objects (See here) and it seems like $scope.companyId is just a number. So your view should look more like this:

<label>Company Name</label>
  <select class="form-control" ng-model="companyId" ng-change="selectCompany()" 
    ng-options="company as company.CompanyName for company in companies">
  {{company.CompanyName}}
</select>

Note the change from company.CompanyId to just company. This should set the ng-model: companyId (should probably be company) to the selected company object. See ng-options doc for details, paying particular attention to the behavior of the select as label for value in array syntax

Hope that helps

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

2 Comments

having a issue with the CompanyId being passed back to the controller error GET localhost:55664/api/apiCompany/[object%20Object] 400 (Bad Request)
ok, well that should now become $http.get('/api/apiCompany/' + $scope.companyId.CompanyId).sucess(...) or (as I alluded to above) if you change $scope.companyId to $scope.company, it's $http.get('/api/apiCompany/' + $scope.company.CompanyId).sucess(...).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.