简体   繁体   English

表格模型绑定Laravel / Angular

[英]Form model binding Laravel / Angular

Is there anny possibility when editing an entry (an update actually) with an Angular form, to populate the fields with the data already stored in the database? 使用Angular表单编辑条目(实际上是更新)时,是否有可能用已存储在数据库中的数据填充字段?

In other words, can I do form model binding with Angular/Laravel? 换句话说,我可以用Angular / Laravel进行模型绑定吗?

If you're going to use ngResource and when you'll get data from Laravel, then you can bind the returned JSON object as a model to your form. 如果要使用ngResource并从Laravel获取数据,则可以将返回的JSON对象作为模型绑定到表单。

    $scope.formDatamodel = {};

    //Suppose you will get data from Laravel here 
    var Apidata = $resource('/api/account/updateStatus').get();

    Apidata.$promise(data){

        $scope.formDatamodel = data;

    }

Now you will have to make ngModel binding in your form controller. 现在,您将必须在表单控制器中进行ngModel绑定。

<input type="password" name="password" ng-model="formDatamodel.password">

I'm not sure if this is the right approch, but this is how I did it. 我不确定这是否合适,但这就是我的做法。

I get the data with the show method: Factory: 我使用show方法获取数据:Factory:

            show : function(
            id
        )
        {
            return $http.get('/api/rides/' + id);
        }

Controller: 控制器:

        $scope.rideEdit = {};
    var id = $stateParams.id;
    // Alles nemen van 1 record
    Ride.show(id)
        .success(function(data) {
            console.log(data);
            $scope.rideEdit.Dataname = data.name;
            $scope.rideEdit.Description = data.description;
            $scope.rideEdit.datetime = data.ridedatetime;
            $scope.rideEdit.persons = data.ridepersons;
            $scope.rideEdit.vertrekpunt = data.ridevertrekpunt;
            $scope.rideEdit.eindpunt = data.rideeindpunt;
        });

So then it displays the data. 因此,它将显示数据。

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

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