路线改变后,控制器中的角度丢失重新连接形式

时间:2014-10-15 14:52:50

标签: angularjs angularjs-scope

我有一个连接到路径的Controller,当页面首次加载模板中的表单时,可以在控制器中轻松访问。表单的名称是form.myForm,所以在控制器中,我可以通过$ scope.form.myform访问表单。一切都很好,除非我走向另一个观点并回来。当我从另一个视图返回到同一视图时,$ scope.form.myForm不再连接到$ scope中的前一个表单。我怀疑,我从视图中断开了一个全新的范围。我不需要在这些视图转换之间维护数据,我只需要能够再次访问该表单而不是完全丢失它。

当routeProvider返回到该模板时,如何在一个$ scope中重新关联现有表单?我是否通过以这种方式访问​​模板中的表单来违反Angular的某些原则?

这是我的路线定义。表单在mytemplate.html中,第一次加载模板时,访问其中的表单没有问题,但如果您导航到另一个路径然后返回,则无法在范围内访问该表单不再。

首次加载视图或刷新浏览器页面。

$scope.form.myForm.$valid //evaluates to true

通过路线更改第二次访问视图

 $scope.form.myForm.$valid //$scope.form is undefined

我的路线定义

routes[ "/myview" ] = {

controller : 'MyController',
templateUrl : 'templates/mytemplate.html',
resolve : {

        ControllerMetaData : function () {
            return {
                    title : "Management",
                    description : "Management"


            }

           }
         }
 };

// myController的

application.controller('MyController', ['$rootScope', '$scope', function($rootScope, $scope) {
    'use strict';

    $scope.form = {}; //If I make this $rootScope, it works on second time back from route change

...

    $scope.pageUp = function(event) {


           //First time through this is all good. after route change and then back to this route, we 
          // no longe have the form anymore 
           if ($scope.form.myForm && $scope.form.myForm.$dirty) 

...

以下是mytemplate.html

中的表单定义
<form name="form.myForm" id="inv" class="forms" role="form"  novalidate>

0 个答案:

没有答案