0

我有一个简单的书店示例,我正在为 angularjs 工作,我正在尝试将书籍 ID 从主页传递到编辑页面上的服务中,以便可以呈现书籍详细信息。我所发生的是home' page with the correct book id being passed into the book service. However, I cannot seem to think of a way to have the,当不同的页面调用休息服务时,我可以看到来自 BookCtrl 的休息调用加载该数据。我期待的顺序是:

1)  User enters a book ID to edit  
2)  User presses Search button  
3)  book.html page is loaded  
4)  BookEdit service is invoked with ID from Steps 1 and 2  
5)  ng-model for book loads data.

提前道歉,当我从另一台计算机修改此代码时可能会出现一些错误,因此我无法复制/粘贴
以下代码:

主页.html

<div ng-controller="HomeCtrl">    
     <div>  
          <label for="query">Book to edit</label>  
          <input id="query" ng-model ="editBook.query">  
          <button ng-click="loadBookById()">Search</button>
     </div>  
</div>  

主页.js:

var homeApp = angular.module('bookHome',['bookEdit']);  
homeApp.controller('HomeCtrl',function($scope,$http,bookEditService)  
{    
    $http.get('http://get/your/books/rest').success(function(data){  
       $scope.library = data;  
    });

    $scope.editBook = {    
      query: '',  
      service:'bookEditService'
    } ;   

    $scope.loadBookById = function()  
    {  
         $scope.$emit('loadBookById',{  
             query:$scope.editBook.query,  
             $service: $scope.editBook .service
    }   

    $scope.$on('loadBookById', function(ev,search){  
         bookEditService.loadBook({  
              bookId: $scope.editBook.query  
           },  
             $scope.searchComplete,  
             $scope.errorSearching  
           );  
        });
    $scope.searchComplete = function(results) {  
             $scope.results = results;  
      };  

    $scope.errorSearch= function(data,status,headers,config){  
          console.log(data);  
          // ...  
    };
}  

book.html

<div ng-controller="BookCtrl" >    

        <div ng-model="details.title"></div>   
        <div ng-model="details.author"></div>
</div>  

bookEdit.js

  var bookEditApp = angular.module('bookEdit',[]);  
    bookEditApp.service('loadBook',function($http){   
        return{  
               loadBookById: function(params,success,error){  
                $http({   
                  url: 'http://path/to/book/editing',  
                  method: 'GET',  
                  params:{bookId: params.bookId}).success(function(data,status,headers,config)  
                  {  
                     var results = data;  
                     success(results || []);  
                 }).error(function(){   
                       error(arguments);  
                });  
            }  
          };  
       });

bookEditApp.controller('BookCtrl',function($scope){    
             $scope.details = {    
              title: "",  
              author: ""
             };  
 });  
4

1 回答 1

2

遵循您期望的顺序的替代方法是:

1)用户输入书籍ID并按下按钮

2) HomeCtrl 将输入的 id 作为路由参数路由到 EditCtrl(还不需要使用 book 服务):

app.controller('HomeCtrl', function ($scope, $location) {

    $scope.editBook = function () {
      $location.path('/edit/' + $scope.id);
    };

  });

3) 加载 EditCtrl,检索路由参数并向图书服务请求正确的图书:

app.controller('EditCtrl', function EditCtrl($scope, $routeParams, bookService, $location) {

    $scope.loading = true;

    bookService.getBookById($routeParams.id)
      .then(function (result) {
        $scope.book = result;
        $scope.loading = false;
      });

4) 加载书籍时,模型 ( $scope.book) 被填充并且 html 被更新

这是一个工作示例,希望能提供一些进一步的指导和想法:http ://plnkr.co/edit/fpxtAU?p=preview

于 2013-11-01T15:51:14.010 回答