简体   繁体   English

AngularJS:在控制器中读取JSON

[英]AngularJS: Reading JSON in controller

I am having some problems exposing a field on my AngularJS controller. 我在AngularJS控制器上暴露一个字段时遇到一些问题。 I have a webservice returning a JSON 'owner' object, with a field called 'Cave'. 我有一个Web服务返回一个JSON“所有者”对象,并带有一个名为“ Cave”的字段。 If this field has a null (or empty/whitespace) value, I want to hide an element in my view using ng-hide. 如果此字段的值为空(或空白),则我想使用ng-hide隐藏视图中的元素。 To achieve this, I hope to expose the field 'hideCave' from the controller, shown in its entirety below: 为此,我希望从控制器中公开字段“ hideCave”,如下所示:

function OwnerController($scope, OwnerFactory, $routeParams) {
    if ($scope.owner == null) {
        $scope.owner = OwnerFactory.getOwner($routeParams.id);
        //$scope.hideCaveat = $scope.owner.Cave == undefined;

        //Outputs 'Hide: {}?undefined'
        alert('Hide: ' + JSON.stringify($scope.owner) + '?' + $scope.hideCaveat); 
    }

    $scope.someButton = function () {
        //Outputs full JSON string when clicked
        alert('Hide: ' + JSON.stringify($scope.owner) + '?' + $scope.hideCaveat); 
    }
}

myApp.factory('OwnerFactory', function ($http) {
    return {
        getOwner: function (id) {
            var url = 'api/Owner/' + id;
            return $http.get(url)
            .then(function (response) {
                return response.data;
            });
        }
    }
});

My data loads fine, but I can't get a value out of $scope.owner.Cave until I add a ng-click event any trigger it. 我的数据可以很好地加载,但是我无法从$ scope.owner.Cave中获取任何值,除非添加任何ng-click事件以触发它。

Example JSON return from web service: 从Web服务返回的示例JSON:

{"FirstName":"David","LastName":"Lynch","Street":"Oak Dr. 7","ZipCode":"90210","Town":"Beverly Hills","Email":"","Cave":"Do not sell this man any more drugs"}

What am I doing wrong since I don't get the value? 因为我没有得到价值,我在做什么错?

Is my issue related to the fact I'm creating a global function for my controller? 我的问题与我正在为控制器创建全局函数有关吗? Should I use myApp.controller instead? 我应该改用myApp.controller吗?

Have you tried using ng-show="owner.Cave" in your template? 您是否尝试过在模板中使用ng-show =“ owner.Cave”? It should work fine. 它应该工作正常。

The reason why you get an empty object alerted is that the $http.get is not a synchronous function. 收到空对象警报的原因是$ http.get不是同步函数。 It returns a promise - so by the time you reach alert you still only have the promise. 它返回一个承诺-因此,当您到达警报时,您仍然只有承诺。 Only once it has been resolved - such as before you click the button, do you see the actual data. 仅在解决问题后(例如在单击按钮之前),您才可以看到实际数据。

You should be able to just access it as you would any other object 您应该能够像访问其他任何对象一样直接访问它

<div class="alert" ng-show="owner.Cave">{{owner.Cave}}</div>

Plunker In the plunker I just hardcoded the data. 柱塞在柱塞中,我只是对数据进行了硬编码。

myApp.factory('OwnerFactory', function ($http) {
    return {
        getOwner: function (id) {
            var url = 'api/Owner/' + id;
            return $http.get(url);
        }
    }
});

and

$scope.owner = OwnerFactory.getOwner($routeParams.id).then(function(data){
     $scope.hideCaveat = data.owner.Cave == undefined;
});

since getOwner returns a promise. 因为getOwner返回了promise。

edit 编辑

better use ng-hide or ng-show on the data you want to show or hide 最好在要显示或隐藏的数据上使用ng-hide或ng-show

<div ng-show="owner.Cave">cave infos</div>

add a watch at beginning of your controller, so "hideCaveat" is always there. 在控制器的开头添加一个手表,因此“ hideCaveat”始终存在。

function OwnerController($scope, OwnerFactory, $routeParams) {
  $scope.$watch( 'owner.Cave', function(cave) {
    if (cave && cave.length > 0) {
      $scope.hideCaveat = false;
    } else {
      $scope.hideCaveat = true;
    }
  });
  ...

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

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