在表单中嵌套ng-view

时间:2022-04-05 12:57:11

given the controller

给控制器

function ctl($scope, $http) {
  $scope.postForm = function() {
    console.log("submitting form")
  }
}

and the view

和观点

  <form name="pform" ng-show="!error.Show">
    <div ng-view></div>
    <button type='button' value='Save' ng-click="postForm()" />
  </form>

The controller method postForm doesn't get called, however, if i move the form tag into the view the method is called. Is there a reason that this doesn't work as I expect it to? Is there another way to accomplish the goal of sharing the form controls across different views?

控制器方法postForm不会被调用,但是,如果我将表单标记移动到视图中,则调用该方法。有没有理由认为这不符合我的预期?是否有另一种方法可以实现跨不同视图共享表单控件的目标?

Update

更新

my module and routeProvider are configured like this:

我的模块和routeProvider配置如下:

angular.module("profilemodule", [])
.config(['$routeProvider', function ($routeProvider) {
    $routeProvider
        .when("/info", { templateUrl: '/partials/profile/info.html', controller: ProfileController })
        .when("/user", { templateUrl: '/partials/profile/user.html', controller: ProfileController })
        .when("/introduction", { templateUrl: '/partials/profile/editor.html', controller: ProfileController })
        .otherwise({ redirectTo: '/info' });
}]).run(function ($rootScope, $location) {
    $rootScope.location = $location;
})

and the page includes some nav elements which are set based on the location service like so:

并且页面包含一些基于位置服务设置的导航元素,如下所示:

<div class="row">
    <div class="offset2 span10">
        <ul class="nav nav-pills">
            <li ng-class="{active: location.$$path.substring(0, '/info'.length) == '/info'}"><a href="#/info" >Information</a></li>
            <li ng-class="{active: location.$$path.substring(0, '/user'.length) == '/user'}"><a href="#/user" >User</a></li>
            <li ng-class="{active: location.$$path.substring(0, '/intro'.length) == '/intro'}"><a href="#/intro" >Introduction</a></li>
        </ul>
    </div>
</div>

<form name="pform" method="POST" ng-show="!error.Show">
   <div ng-view></div>
   <button type='button' value='Save' ng-click="postForm()" />
</form>

the ng-class statements works perfectly, is it because I've set the location property of $scope in the module's run method?

ng-class语句工作得很好,是因为我在模块的run方法中设置了$ scope的location属性?

thanks,

谢谢,

jason

贾森

1 个解决方案

#1


1  

ng-view with routing creates a new scope with the controller, and you can't reach a child scope. Your submit action lies in the parent scope and the form data lies in the child scope (created by ng-view).

带路由的ng-view使用控制器创建新范围,并且无法访问子范围。您的提交操作位于父范围,表单数据位于子范围(由ng-view创建)。

If you want to use common form controls, you can use ng-include, this directive gets template it and renders that in the current scope.

如果要使用常用表单控件,可以使用ng-include,此指令获取模板并在当前范围内呈现它。

Move your form controls to a new template, then include them in all of your forms.

将表单控件移动到新模板,然后将它们包含在所有表单中。

API reference:
http://docs.angularjs.org/api/ng.directive:ngInclude

API参考:http://docs.angularjs.org/api/ng.directive:ngInclude

#1


1  

ng-view with routing creates a new scope with the controller, and you can't reach a child scope. Your submit action lies in the parent scope and the form data lies in the child scope (created by ng-view).

带路由的ng-view使用控制器创建新范围,并且无法访问子范围。您的提交操作位于父范围,表单数据位于子范围(由ng-view创建)。

If you want to use common form controls, you can use ng-include, this directive gets template it and renders that in the current scope.

如果要使用常用表单控件,可以使用ng-include,此指令获取模板并在当前范围内呈现它。

Move your form controls to a new template, then include them in all of your forms.

将表单控件移动到新模板,然后将它们包含在所有表单中。

API reference:
http://docs.angularjs.org/api/ng.directive:ngInclude

API参考:http://docs.angularjs.org/api/ng.directive:ngInclude