0

Is this even possible with jquery? My javascript code works in terms of returning data with the get statement, and after debugging I verified that the uploadCategories did indeed contain the requested objects, but for whatever reason they are not being rendered to the select tag. What am I missing here?

Javascript:

var uploadController = function($scope) {

// upload category array
$scope.uploadCategories = [];
$scope.category = "";

// get categories
$.get('/api/Upload/UploadCategories', function(data) {

    // update array
    $scope.uploadCategories = [];
    $.each(data, function(i, item) {
        $scope.uploadCategories.push({
            Id: item.Id,
            Category: item.Category
        });

        // set active category
        $scope.category = $scope.uploadCategories[0].Category;

    });
}, "json");
};

HTML:

<form class="text-center" action="/Upload/AdminUpload" method="post" 
  data-ng-controller="uploadController">

<fieldset class="form-group" style="display: inline-block;">
    <legend>File Info</legend>

    <!--Upload Category-->
    <div class="form-group">
        <label for="catId">Category</label>
        <select id="catId" ng-model="category" 
                ng-options="c.Category for c in uploadCategories"></select>
    </div>


    <button type="submit" formenctype="multipart/form-data">Upload</button>
</fieldset>

1
  • Although it is tempting to use jQuery with angular. 90% of the time it is unnecessary and results in you writing more code than you need. Use the built in modules as KayKay suggests. Other than that $scope.apply(function(){}); is the way to go. Commented Apr 26, 2014 at 1:51

1 Answer 1

1

i think you need to call the $scope.$digest() function after setting the category. This makes angularJS refresh the view from the model. It is needed because your callback is in JQuery, so angular does not call the digest automatically, whereas if you use angular services instead :

$http.get('/api/Upload/UploadCategories').success(function(data){..});

the digest is implicitly be called at the end, so the refresh is automatic.

A more conventionnal way than calling the digest (but equivalent), is surrounding your callback code in a $scope.apply(function(){});, this executes your code and then calls digest.

But still the better way would be to use angular's $http.get() as it comes from the box.

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

2 Comments

That solved it thanks. So since staying in box is the recommended route, what about the jquery $.each function. Is there a preferred angular alternative?
Yes there is the 'angular.forEach()' function. But it is generally not a problem to use other libraries with angular. What matters is who triggers the function call. When it's angular everything will work automatically because the '$scope.$digest()' is called at the end by the framework. But when the trigger is not angular, then the '$scope.$digest()' is not called (because the library does not know about angular) so you have to call it manually.

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.