AngularJS:为什么解析数据不会在视图上显示?

时间:2015-07-22 02:45:33

标签: angularjs angularjs-directive angularjs-scope angularjs-ng-repeat

我正在尝试解析json文件,并希望在视图上显示。我能够在控制台上显示数据,但我无法在视图上显示。我正在使用ng-repeat。但是,它没有用。

另外,我尝试了 $ scope。$ apply ,但是它已经运行了错误和摘要周期,但数据没有显示,为什么?

以下是我的plunker链接:

http://plnkr.co/edit/uyyhxex5gubzTJSZipV0?p=preview

HelloWorldService.doWork().then(function(data){
     console.log("data received to Ctrl");

    $scope.data=data.employees;
     $scope.hide();
    // $scope.$apply();

   })

3 个答案:

答案 0 :(得分:1)

在服务中,您应该使用angular.fromJson(e.data)将String转换为Json,它可以正常工作。参见工作示例

http://plnkr.co/edit/J3p13z99gRBlHmuSal1c?p=preview

答案 1 :(得分:0)

$scope.data = JSON.parse(data).employees

答案 2 :(得分:0)

您的tableView是一个字符串。您需要先使用heightForRowAtIndexPath对其进行解析,然后才能使用它。

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    print("idx = \(indexPath.row)")

    return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}