Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了angularjs – 在表单内嵌套ng-view大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
给控制器

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

和观点

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

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

更新

我的模块和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;
})

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

<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>

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

谢谢,

贾森

解决方法

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

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

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

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

大佬总结

以上是大佬教程为你收集整理的angularjs – 在表单内嵌套ng-view全部内容,希望文章能够帮你解决angularjs – 在表单内嵌套ng-view所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: