1

I'm trying to make 2tabs app using routeProvider ng-view and 2 controllers.At first tab would be user form. At second tab i want to show back to user what he entered with validation messages.

routing

project.config(["$routeProvider",function($routeProvider)
    {
    $routeProvider.
        when('/', {controller:'Ctrl1', templateUrl:'tab_1.html'}).
        when('/tab1', {controller:'Ctrl1', templateUrl:'tab_1.html'}).
        when('/tab2', {controller: 'Ctrl2', templateUrl:'tab_2.html'}).

To store data i use factory

project.factory('userService', function(){
    return{valids: {}}
})

and call it on my 2 cntrollers

project.controller('Ctrl1', ['$scope', '$rootScope', 'userService', function(scope, rootScope, userService){
scope.data=userService;

And assign variables on controller html template

ng-model="data.input1"

After page(tab1) is loaded i have access to $dirty and $invalid values of my input.

scope.form.input.$invalid;

But when user switches to tab2 as i understand controller1 flushes, and i now ave only factory data and some vars that is send manually using $on('$routeChangeStart' function

The question is - what is the best and correct way to store/send/share these $dirty and invalid variables, to have access to them in second controller? How to do it angular way?

Mb i need add them to factory data like so

scope.data.valids.input_invalid = scope.form.input.$invalid;
scope.data.valids2.input_invalid = scope.form.input2.$invalid;

Or add some controller(factory/directive?)?

Or manually write validation function(controller/factory/directive?) on controller2/2 scope?

4

1 回答 1

1

曼尼 D 是对的。解决问题的最佳方法是创建一个服务来存储输入,并将其注入每个控制器。看起来您可以为此目的调整 userService 。

在处理输入的控制器中,您可以 $watch 表单,并在服务上调用 setIsValid(scope.form.input.name, scope.form.input.$valid) 方法来存储表单更改时的状态。将每个输入的有效性存储在服务中的对象上。

在另一个控制器中,您可以在范围上添加对此服务的引用,然后将数据绑定到输入有效性对象的属性以在您的 UI 中显示它们。

于 2013-10-18T15:47:28.543 回答