如何读取和显示 $scope-object 中的内容?

How can I read and show what's inside of a $scope-object?

这个问题可能有点愚蠢,但我想知道...

$scope.rma.retailerRetailerId = retailerService.get({id: retailerNumber});
console.log("address-1--->" + $scope.rma.retailerRetailerId.address);

----> 在控制台中:address-1--->undefined

但是在view/html中是正常的,这是为什么呢?如何查看控制器中对象的内部内容?为什么它告诉我它是未定义的,即使里面有数据?

<input type="text" class="form-control" ng-model="rma.retailerRetailerId.address" placeholder="Address">

更新 - 我添加了服务代码

angular.module('retailerService', ['ngResource'])
    .factory('retailerService', ['$resource',
        function ($resource) {
            return $resource(

                    'http://localhost:8080/RmaServer/webresources/com.rako.rma.retailer/:id',
                    {},
                    {

                        update: { method: 'PUT', params: {id: 'retailerNumber'} }
                    });
        }]);

如果你想从控制台访问 $scope,你需要使用 jQuery 插件 scope(由 Angular 添加)。

console.log("address-1--->" + $('.form-control').scope().rma.retailerRetailerId.address));

我认为这也行得通:

console.log("address-1--->" + angular.element('.form-control').scope().rma.retailerRetailerId.address));

我认为您的 console.log 不知道您的对象是什么,因为 GET 方法是异步的,因此当您尝试调用您的 console.log.. 尝试处理时,您的地址仍然未定义您在 GET 回调中的地址。

retailerService.get({id: retailerNumber})
  .success(function(data) {
    console.log(data.adress)
    $scope.adress = data.adress //(example, idk how you manage your object)
  })

你可以用这个做不同的事情:

$scope.adress = retailerService.get({id: retailerNumber})

在你的 html 中你可以这样使用:

ng-model='adress.adress' //(ok here, it's really ugly :) )

希望对你有所帮助。