0

I have multidimensional json:

{"cat":
        {"total":12,"per_page":3,"current_page":1,"last_page":4,"from":1,"to":3,
         "data":[{"id":1,"emlakKategori":"a"},{"id":2,"emlakKategori":"b"},{"id":3,"emlakKategori":"c"}]}}

And i am reaching to data via ng-repeat like this:

    <tr ng-repeat="kat in kategoriList.cat.data">
        <td>{{ kat.id }}</td>
        <td>{{ kat.emlakKategori }}</td>

My service is:

   app.factory('KategoriData',['$resource', 'api.config', function($resource, config) {
return $resource(config.apiBasePath + 'kategori',{}, {
    query: {
        isArray: false,
        method: 'GET'
    }
});

And my controller:

kategori.controller('KategoriListCtrl',['$scope', 'KategoriData', function($scope, KategoriData) {
    $scope.kategoriList = KategoriData.query();

}

I wonder how can i assign data to variable inside controller and reach it in view with directly kategoriList instead of kategoriList.cat.data

Thanks.

2 Answers 2

1

You can make the assignment in your controller in the success callback of your query.

kategori.controller('KategoriListCtrl',['$scope', 'KategoriData', function($scope, KategoriData) {
    KategoriData.query({},
         function(data){
             $scope.kategoriList = data.cat.data;
         });
}
Sign up to request clarification or add additional context in comments.

Comments

1

Actually your example is not valid anymore due to this (since v1.2.0-rc.3 )

deprecate promise unwrapping means that you can not bind a scope value to a promise and when the promise will be resolved a $digest will automatically unwrap it.

You should use a callback:

kategori.controller('KategoriListCtrl',['$scope', 'KategoriData', function($scope, KategoriData) {
  KategoriData.query({}, function(data){
    $scope.kategoriList.cat.data = data;
  })
}  

Comments

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.