Javascript 访问另一个对象的对象

Javascript 访问另一个对象的对象,javascript,angularjs,Javascript,Angularjs,我想访问另一个对象中的对象,但每当我控制台它时,它都会显示“未定义”。 范例 如何从“empresaInstrumento”中提取此对象“instrumento”,而不丢失其引用 (get方法从api检索信息)这是因为函数empresaInstrumento没有名为instrumento的属性 话虽如此,一个更好的方法是做以下工作: $scope.instrumento = new Instrumento(); $scope.empresaInstrumento = { // create

我想访问另一个对象中的对象,但每当我控制台它时,它都会显示“未定义”。 范例

如何从“empresaInstrumento”中提取此对象“instrumento”,而不丢失其引用


(get方法从api检索信息)

这是因为函数
empresaInstrumento
没有名为
instrumento
的属性

话虽如此,一个更好的方法是做以下工作:

$scope.instrumento = new Instrumento();

$scope.empresaInstrumento = {
 // create an instrumento property
   instrumento:  function() {
      var toReturn = {};
      EmpresaInstrumento.get({id:$routeParams.id}, function (instrumento) {
           toReturn = instrumento.instrumento;
        });

      return toReturn;
    }
}
console.log($scope.empresaInstrumento.instrumento());

在代码中,您没有
函数
$scope.empresaInstrumento
内运行,您必须将其设置为
函数
,然后从控制器或视图中调用它为
ng init
,请参见示例

app.controller("ctrl", ["$scope", "EmpresaInstrumento", "$routeParams", function ($scope, empresaInstrumento, routeParams) {
    $scope.instrumento = {};

    $scope.empresaInstrumento = function () {
        empresaInstrumento.get({ id: routeParams.id }, function (instrumento) {
            $scope.instrumento = instrumento.instrumento;
            console.log($scope.instrumento);
        });
    }

    $scope.empresaInstrumento(); //or in ng-init="empresaInstrumento()"

    console.log($scope.instrumento);
}]);

我猜您在html页面中没有看到范围值被更新,您添加了console.logs来测试该值是否正确设置

您有两个问题:

1-在API调用返回任何数据之前,将执行第二个console.log。这就是为什么你没有定义。(但第一个日志应显示正确的值,因为它位于promise块内

2-根据EmpresaInstrumento.get的编码方式,可能需要使用$scope.$apply()更新作用域值


但是,如果第一个console.log显示未定义,则表明您的服务存在问题,我们需要查看如何对其进行编码以进行调试。

正如Maher在那里所说,我使用局部变量捕获响应,然后我能够在回调中调用命名函数保存该信息,以发送到在f中创建的$scope变量使用局部变量执行函数

大概是这样的:

$scope.empresaInstrumento=函数(){
empresaInstrumento.get({id:routeParams.id},函数(instrumento){
$scope.instrumento=instrumento.instrumento;
$scope.loadInstrumento($scope.instrumento);
});
}
$scope.loadInstrument=函数loadInstrument(元素){
$scope.instrumento=元素;

}
您的控制台是否安装了instrumento并检查了它是否安装了instrumento?是的,安装了。“empresaInstrumento”显示了对象“empresa”和“instrumento”。当我控制台empresaInstrumento.instrumento一直显示“undefined”,但在该函数中它正确显示了Instrument angular的哪个版本?您的服务empresaInstrumento.get是否使用$http或自定义承诺?谢谢!我这样做似乎更令人愉快。我将稍后在重构代码时重试
app.controller("ctrl", ["$scope", "EmpresaInstrumento", "$routeParams", function ($scope, empresaInstrumento, routeParams) {
    $scope.instrumento = {};

    $scope.empresaInstrumento = function () {
        empresaInstrumento.get({ id: routeParams.id }, function (instrumento) {
            $scope.instrumento = instrumento.instrumento;
            console.log($scope.instrumento);
        });
    }

    $scope.empresaInstrumento(); //or in ng-init="empresaInstrumento()"

    console.log($scope.instrumento);
}]);
$scope.instrumento = new Instrumento();

$scope.empresaInstrumento = EmpresaInstrumento.get({id:$routeParams.id}, function (instrumento) {
   $scope.instrumento = instrumento.instrumento;
    console.log($scope.instrumento);
    $scope.$apply(); // update the scope and your view
});
console.log($scope.instrumento); // still undefined here