简体   繁体   English

如何在AngularJS中访问AngularJS对象

[英]how to Access AngularJS object in AngularJS

This is my Angular code. 这是我的Angular代码。 I am getting an array of userInfo through AngularJS services. 我正在通过AngularJS服务获取一组userInfo

app.controller('personalController', function ($scope, $http, $window, userInfo) {

    $scope.info = userInfo.getValue();
    console.log( $scope.info);
    //$scope.data = $scope.info;
    console.log($scope.info.inf.CNIC);
}

This is my Angular service: 这是我的Angular服务:

app.service('userInfo', function () {
    var infor = {};
    this.setValue = function (info) {
        infor["inf"] = info;
    }
    this.getValue = function (info) {
        return infor;
    }
})

I am getting the following values: 我得到以下值:

在此处输入图片说明

And I am getting cannot read the property error in console.log($scope.info.inf.CNIC); 而且我越来越cannot read the property console.log($scope.info.inf.CNIC); cannot read the property错误console.log($scope.info.inf.CNIC); .

Here's what I do to reach to my angularjs objects. 这是我要达到我的angularjs对象的工作。

In your controller you set 在您的控制器中进行设置

window.test = $scope;

Then in your developer console you can reach to your angular objects like below 然后,在开发人员控制台中,您可以到达如下所示的角度对象

test.info

If you are using Chrome you can do the following: 如果您使用的是Chrome,则可以执行以下操作:

Select the element you want to get the scope from via right-clicking and Inspect Element on from the browser, or selecting the element from the Elements tab of the Dev Tools, this will cause $0 to point to the element when referenced in the Dev Tools console. 通过右键单击并在浏览器中检查元素来选择要获取作用域的元素,或者从开发工具的“元素”选项卡中选择该元素,这将导致$ 0指向在开发工具中引用的元素安慰。

In dev tools console you can use the below statement and get the scope: 在开发工具控制台中,您可以使用以下语句并获取范围:

angular.element($0).scope();

So to recap: 总结一下:

  1. Right click on the element you want to get the scope and click inspect Element 右键单击要获取范围的元素,然后单击检查元素
  2. type var sc = angular.element($0).scope(); 类型var sc = angular.element($ 0).scope();
  3. Check the data inside sc variable 检查sc变量中的数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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