使用ngResource

时间:2015-07-19 10:44:57

标签: angularjs laravel-5

我的HTML块中有两个输入字段ng-model="book_id"ng-model="borrower_id"。我还有一个调用commit()的按钮。每当我点击它时,我希望在一次执行中保存两个id。这在Angular中可能吗?我试过搜索,但找不到任何东西。如果有人有想法我会非常感激。

到目前为止,这是我的代码片段。

/app.js /

$scope.commit = function(){
            $scope.$emit('Load')
            Transaction.save({
                'book_id': book.id,
                'borrower_id': lender.lenderID
            }).$promise.then(function(data){
                    if(data.msg){   
                    //angular.copy({}, $scope.book);    
                    $scope.$emit('Unload');
                    $window.location.href='/';
                }
            })  

        };

/ API / TransactionController /

class TransactionController extends Controller{

public function store(Request $request){



    $transaction = new Transaction();

    $transaction->book_id = $request->book_id;
    $transaction->lender_id = $request->borrower_id;

    $transaction->save();

    return response()->json([
        'msg' => 'Success',
        '' => $transaction
        ]);
}

}

/ HTML /

<label for="borrowerID"><span class="text-success">Borrower ID</span>   </label>
                                <i ng-show="loading" class="fa fa-spinner fa-1x fa-spin"></i>   
                                <input  type="password" name="borrowerID" id="borrowerID" class="form-control" ng-model="lender.lenderID" disabled>

/--book_id--/

<label for="bookID"><span class="text-success">Book ID</span></label>
                                <i ng-show="loading" class="fa fa-spinner fa-1x fa-spin"></i>   
                                <input type="password" name="bookID" id="bookID" class="form-control" ng-model="book.id" disabled>

/型号/交易/

class Transaction extends Model
{

protected $table = 'transactions';

protected $fillable = ['book_id', 'borrower_id'];


public function lenders(){

    return $this->belongsTo('\App\Lender');

}
}

1 个答案:

答案 0 :(得分:0)

我认为当您将数据传递给api调用时,可能会遗漏$scope。假设您在与您提供的HTML模板关联的控制器中有此功能。这有用吗:

$scope.commit = function(){
            $scope.$emit('Load')
            Transaction.save({
                'book_id': $scope.book.id, // added $scope 
                'borrower_id': $scope.lender.lenderID // added $scope 
            }).$promise.then(function(data){
                    if(data.msg){   
                    //angular.copy({}, $scope.book);    
                    $scope.$emit('Unload');
                    $window.location.href='/';
                }
            })  

        };