使用laravel,ajax和angularJs更新多个select2字段

时间:2015-12-22 08:40:11

标签: javascript php angularjs ajax laravel

我有一个多选的select2选择字段 我想要的是在我的选择字段中添加一个新值。 (在向数据库添加条目并将此条目绑定到选择字段后)

到目前为止,这是我的代码:

刀片模板:

<div class="form-group {{ ($errors->first('projectTypes')) ? 'has-error' : '' }}">
 <label for="projectTypes" class="col-md-2 control-label">{{ trans('project/project.fields.projectTypes') }}</label>
 <div class="col-md-9">
     {!! Form::select('projectTypes[]',
        $projectTypes,
        ($project->projectTypes()->count() > 0) ? $project->projectTypes->lists('id')->toArray() : (isset($projectType)) ? $projectType->id : '',
        ['id' => 'projectTypes', 'class' => 'form-control', 'ng-model' => 'projectTypes', 'multiple' => 'multiple'])
      !!}
      <span id="helpBlock" class="help-block">{{ $errors->first('projectTypes') }}</span>
  </div>
  <div class="col-md-1">
    <i class="fa fa-plus-square-o fa-2" data-toggle="modal" data-target="#addProjectType"></i>
  </div>

<i>标记切换模态。在模态窗口中,我有一个输入字段和一个按钮(它可以工作!):<​​/ p>

<input ng-model="name" id="addProjectType" class="form-control" type="text" name="name" placeholder="Projekttyp">
<button type="button" class="btn btn-primary btn-embossed" ng-click="addProjectType(name)" data-dismiss="modal">Save changes</button>

因此,如果我点击按钮,这将触发我的angularJS代码中的一个功能(这有效!) 这是我的angularJS代码:

$scope.projectTypes = [];
$scope.addProjectType = function(name) {
    $http.post('/ajax/projecttype/addprojecttype', {
        name: name
    }).success(function(data, status, headers, config) {
        $scope.projectTypes.push(data);
    });
}

这是对我的laravel php函数的ajax调用(这个有效!):

public function addProjectType(Request $request, Sentinel $sentinel) {
    $name = $request->get('name');
    $projectType = new ProjectType;
    $projectType->name = $name;
    $projectType->slug = $projectType->strSlug($name);
    $this->created_by = $sentinel::getUser()->id;
    $this->updated_by = $sentinel::getUser()->id;
    $projectType->saveModel($request);
    return Response::json($projectType, self::$OK);
}

但是我已经宣布了我的选择字段(ng-model="projectTypes") 我宣布$scope.projectTypes并在ajax调用后将新对象推送到$scope.projectTypes,选择字段将不会更新。

我在这里做错了什么?

0 个答案:

没有答案