使用Laravel在Angularjs中未定义范围

时间:2015-03-29 14:57:35

标签: laravel angularjs-scope

我正在使用angular来验证用户输入。然后我将表单发布到Laravel进行验证,如果仍有错误,则表单会被输入重定向。 问题是:我收到错误'ReferenceError:scope is not defined',但是根据需要形成函数。 问题是:我应该如何定义角度范围,当重定向此表单时,表单在第一次加载时显示为空,并在输入时使用输入形式。

这是形式:

        <div data-ng-app="register" class='e1 well mid'>
         <div data-ng-controller="registerChef">
          <legend>Register Chef</legend>
            {{Form::open(array('url'=>'register_chef2', 'name'=>'regchef', 'data-ng-submit'=>'true', 'novalidate'=>'novalidate', 'method'=>'POST', 'class'=>'form-horizontal')) }}
       <div class="form-group">
        {{Form::label('fname','Name : ', array('class'=>'control-label col-sm-4 tl'))}}
        <div class='col-sm-8'>
        {{Form::text('chef_first_name','',array('id'=>'first_name','data-ng-model' =>'chef.first_name','class'=>'form-control input-md','data-ng-minlength' =>'2', 'data-ng-maxlength' => '20' , 'data-ng-pattern'=>'/^[a-z0-9]+[_.-]?[a-z0-9]+$/i','data-ng-model-options'=>'{ debounce: { "default" : 500 } }','required'))}}

                <small class="error" 
                    data-ng-if=" regchef.chef_first_name.$error.required && regchef.chef_first_name.$dirty">
                    Your name is required.
                </small>
                <small class="error" 
                        data-ng-if="regchef.chef_first_name.$error.minlength && regchef.chef_first_name.$dirty ">
                        Your name is required to be at least 2 characters long
                </small>
                <small class="error" 
                        data-ng-if=" regchef.chef_first_name.$error.maxlength && regchef.chef_first_name.$dirty">
                        Your name cannot be longer than 20 characters
                </small>
                <small class="error" 
                        data-ng-if="!regchef.chef_first_name.$error.minlength && regchef.chef_first_name.$error.pattern">
                        Your name cannot contain spaces and special characters
                </small>
        </div>
    </div>

    <div class="form-group">
    <br />
    {{ Form::submit('Register', array('class'=>'btn btn-success btn-lg col-md-3 col-md-offset-2 col-xs-3 col-xs-offset-2 col-lg-3 col-lg-offset-2'))}}
    {{HTML::link('owner_home','Back',array('class'=>'btn btn-primary btn-lg col-md-3 col-md-offset-2 col-xs-3 col-xs-offset-2 col-lg-3 col-lg-offset-2'))}}
    </div>
    {{Form::close()}}
    </div>
</div>

角度控制器:

    var app = angular.module('register',[]);
    app.controller('registerChef',['$scope', function($scope){


    $scope.chef = {
        rest_id : scope.rest_id,
        first_name : scope.first_name,
        last_name : scope.last_name,
        login_name : scope.login_name,
        email : email
    }
    }]);

Laravel控制器:

    public function post_registerChef2(){
           $title = 'Register Chef';
           $validation = ChefUser::validate(Input::all());
           if($validation->fails()){
              return Redirect::to('register_chef')
            ->withErrors($validation)
            ->withInput(Input::except('chef_password','chef_confirm_password'));
    }
    return View::make('owner.reg_chef2')
        ->with('title', $title);
}

Laravel做路由。

2 个答案:

答案 0 :(得分:0)

您的javascript代码中有错误=&gt;

 $scope.chef = {
        rest_id : =>scope.rest_id,
        first_name : =>scope.first_name,
        last_name : =>scope.last_name,
        login_name : =>scope.login_name,
        email : email
    }

答案 1 :(得分:0)

我的问题的答案很简单:

在Laravel控制器中输入到对象

    ->with('chef',(object)Input::except('password','password_confirmation'));

然后在角度控制器中定义json的范围来自此对象,或者如果对象不存在则定义空范围。

像这样:

     var scope = <?php 
                if(null !== Session::get('chef')){
                        echo '{';
                        echo ' "first_name" : "'.Session::get('chef')->first_name.'",';
                        echo ' "last_name" : "'.Session::get('chef')->last_name.'",';
                        echo ' "login_name" : "'.Session::get('chef')->login_name.'",';
                        echo ' "email" : "'.Session::get('chef')->email.'"';
                        echo '}';       
                    }else{
                        echo '{}';
                    }
                ?> ;

       $scope.chef = {
         rest_id : scope.rest_id,
         first_name : scope.first_name,
         last_name : scope.last_name,
         login_name : scope.login_name,
         email : email
       }

可能有更有效的方法如何做到这一点,但这就是我如何运作。