简体   繁体   中英

AngularJS access object field in controller

So i got this AngularJS controller where i need to use 2 different services Project and Subproject . One is linked to the other by project_id field. The code is as follows:

bonsControllers.controller('SubprojetDetailCtrl', ['$scope', '$routeParams', 'Project', 'Subproject',
  function($scope, $routeParams, Project, Subproject) {

    $scope.subproject = Subproject.queryFalse({projectId: $routeParams.id});     

    $scope.project = Project.queryFalse({projectId: $scope.subproject.project_id});
}]);  

The problem is when I use in $scope.project or even using it on console.log, the value for $scope.subproject.project_id is undefined.

The services.js code is :

    var bonsServices = angular.module('bonsServices', ['ngResource']);

    bonsServices.factory('Project', ['$resource',
    function($resource) {
        return $resource(URL_API + '/projects/:projectId', {}, {
            queryTrue: {method: 'GET', params: {projectId: ''}, isArray: true},         
            queryFalse: {method: 'GET', params: {projectId: ''}, isArray: false},           
            save: {method: 'POST', headers: {'Content-Type': 'application/json'}, isArray: false},
            'delete': {method: 'DELETE', params: {projectId: ''}, isArray: false},
            update: {method: 'PUT', params: {projectId: ''}, headers: {'Content-Type': 'application/json'}, isArray: false}
        });
    }]);


bonsServices.factory('Subproject', ['$resource',
    function($resource) {
        return $resource(URL_API + '/subprojects/:projectId', {}, {         
            queryFalse: {method: 'GET', params: {projectId: ''}, isArray: false}
        });
    }]);    

And the template code is :

<div >
    <span>Client :</span> {{project.client.name}} <br />
    <span>Projet :</span> {{project.name}} <br />
    <span>Sous Projet :</span> {{'Saison ' + subproject.saison}} <br />
    <span>Type :</span> {{project.type}} {{subproject.project_id}}<br />
    <span>Nature :</span> {{subproject.nature}} <br />
</div>

In Firebug I can see clearly all $scope.subproject's elements, including project_id

I would very much apreciate if you guys can give me a hint on this issue.

Thanks

Sure, because the Subproject service has an async query there. It will return a promise, when it's done.

bonsControllers.controller('SubprojetDetailCtrl', ['$scope', '$routeParams', 'Project', 'Subproject',
  function($scope, $routeParams, Project, Subproject) {

    Subproject.queryFalse({projectId: $routeParams.id})
    .then(function(subproject){
      $scope.subproject = subproject;
      return Project.queryFalse({projectId: $scope.subproject.project_id});
    }).then(function(project){
      $scope.project = project;
    });           
}]); 

And the reason why firebug logs all elements, because it will log a reference to the objects, the console log then refreshes the object data, when the query is done. So for some milliseconds the data is undefined and will be populated when query is done.

To be sure, that the console.logged data is there, just write $scope.subproject.project_id

The following code worked for me and it follows Konstantin's answer :

bonsControllers.controller('SubprojetDetailCtrl', ['$scope', '$routeParams', 'Project', 'Subproject',
  function($scope, $routeParams, Project, Subproject) 
  {

    var project;
    $scope.subproject = Subproject.queryFalse({projectId: $routeParams.id});
    $scope.subproject.$promise.then(function(subproject){
        project = Project.queryFalse({projectId: subproject.project_id});  
        $scope.project = project;
    }); 
  }
]);

As you might see $promise.then does the trick for me.

Thank you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM