如何从response.data?

时间:2017-04-04 09:08:36

标签: javascript angularjs

Foloowing是我的控制器,用于显示json记录集合中的单本书详细信息

.controller('BookDetailsController', ['$scope','$http','$stateParams',function($scope,$http,$stateParams){
        $http({
            url: "/api/books/",
            method: "get",
            params: {id: $stateParams.id}
        }).then(function(response){
            $scope.books = response.data;
            console.log($scope.books);

        })
}]);

<div class="panel panel-default">
    <div class="panel-heading">Online Bookstore</div>
    <div class="panel-body">
    <div class="col-md-12">
        <div class="col-md-3">
        <img src="{{book.imgUrl}}" class="img-thumbnail" width="200" height="200">
        </div>
        <div class="col-md-9">
            <h2>{{book.title}}</h2>
            <p style="text-align:justify;">{{book.description}}</p>
        </div>
    </div>
    <hr>
    </div>
  </div>

如何从response.data显示单本书记录;使用上面的控制器

4 个答案:

答案 0 :(得分:1)

如果class public_figure: def save_public_figure_page(self, type, p_f_name): glovar.date = time.strftime("%Y%m%d", time.localtime()) p_f_name = p_f_name.trim() # Trim the name to get rid of extra spaces p_f_page_name = '{t}_{pfn}.html'.format(t=type, pfn=p_f_name) p_f_page_file_directory = os.path.join( directory, # Add the directory from the data_storage.directory property "dataset", "html", type, glovar.date, p_f_name, ) if data_storage.directory_create(self.p_f_page_file_directory): html_user_page = glovar.webdriver_browser.page_source p_f_page_file = os.path.join(p_f_page_file_directory, p_f_page_name) html_file = open(p_f_page_file, "w", encoding='utf-8') html_file.write(html_user_page) html_file.close() 是数组,则可以

1)循环数组以显示所有书籍

#create the file storage directory
class data_storage:
    def directory_create(self, path):
        self.directory = os.path.join(os.path.dirname(__file__), path)
        if not os.path.exists(self.directory):
            try:
                os.makedirs(self.directory)
            except:
                raise
            else:
                return True
        else:
            return True

2)仅从数组

中的位置显示一本书
$scope.books

答案 1 :(得分:0)

循环显示外部import { Component } from '@angular/core'; import { OdooRPCService } from 'angular2-odoo-jsonrpc'; import { Http} from '@angular/http'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], providers: [OdooRPCService] }) export class AppComponent { constructor( private odooRPC: OdooRPCService){ this.odooRPC.init({ odoo_server: "https://server.name" }); this.odooRPC.login('xx', 'admin', 'admin').then(res => { console.log('login success'); }).catch( err => { console.error('login failed', err); }) } } 中的books

<div>

要显示单个记录(比如索引.... <div class="col-md-12" ng-repeat="book in books"> .... ),请在视图中使用0

books[0]

答案 2 :(得分:0)

只需取出数据中的第一个对象,

$scope.book = response.data[0];

答案 3 :(得分:0)

如果$ scope.books是一个数组,那么你可以使用@mistails的ng-repeat作为上面的代码。它将简单地动态创建一个书籍列表。从列表中您可以选择特定的书籍,您可以通过ID来获取图书的整个数据。

<div ng-repeat="b in books" data-ng-click="showBookData(b)"> {{b.title}} <img ng-src="{{b.imgUrl}}" ... </div> showBookData()  在点击上你可以打开一个模态并显示总的详细信息。