【问题标题】:Get object value returned from mongo query in same controller在同一控制器中获取从 mongo 查询返回的对象值
【发布时间】:2015-11-10 16:01:19
【问题描述】:

如何获取从 mongodb 查询返回的对象并在同一个控制器中使用它?目前我无法从中获取对象属性。例如我的对象有一个月,下面是一个代码示例:

budgetsApp.controller('BudgetsCostbreakdownController', function($scope, $stateParams, $http, $location,$modal,$log, Authentication, Budgets) {

    // Get current budget
    $scope.budget = Budgets.get({ 
                budgetId: $stateParams.budgetId
            });

    $scope.chartObject.options = {
        'title': 'My Budget and Expenses for month of'+$scope.budget.budgetMonth //wont appear in my view .
    };
        //end chart
    }
);

【问题讨论】:

    标签: angularjs mongodb


    【解决方案1】:

    为此目的使用回调Resource.action([parameters], [success], [error])

    // Get current budget
    Budgets.get({budgetId: $stateParams.budgetId}, function(response) {
        $scope.chartObject.options = {
            'title': 'My Budget and Expenses for month of'+ response.budgetMonth //wont appear in my view .
        };
    });
    

    【讨论】:

      【解决方案2】:

      假设您的 Budgets 对象等于代码中某处定义的 $resource,您需要“等待”直到数据从 RESTful 后端到达。

      // Get current budget
      Budgets.get({ 
        budgetId: $stateParams.budgetId
      }).$promise.then(function(result){
        $scope.chartObject.options = {
          'title': 'My Budget and Expenses for month of' + result.budgetMonth
        };
      });
      

      【讨论】:

        猜你喜欢
        • 2020-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-14
        • 1970-01-01
        • 1970-01-01
        • 2012-08-16
        相关资源
        最近更新 更多