【问题标题】:Use Modal as a template view and reference to it使用 Modal 作为模板视图并引用它
【发布时间】:2016-01-29 06:08:18
【问题描述】:

我在 nav.html 中包含了模态代码(如下),它按预期工作(登录、注销...工作)。

  <div class="modal fade" id="authModal" role="dialog">

<!-- Modal content-->
<div class="modal-content">              
  <div class="modal-body">
    <form class="form-signin" role="form">
      <input ng-model="user.email" type="email" class="form-control" placeholder="Email address" required="" autofocus="">
      <input ng-model="user.password" type="password" class="form-control" placeholder="Password" required="">
      <div class="checkbox checkbox-success">
        <input ng-model="checkbox.signup" ng-init="checkbox.signup=false" type="checkbox">
        <label> Sign Up for first-timer </label>
      </div>
      <div class="text-center">
      <button ng-click="login($event)" class="btn btn-lg btn-primary" type="button">Sign In</button>
      </div>
    </form>
  </div>
</div>

但是当我将所有模态内容移动到名为 md.html 的文件并通过

将其包含到 nav.html 中时
<div class="navbar-header" ng-controller="MainCtrl">
   <div ng-include="'views/modals/md.html'"></div>
</div>

我绝对将它包含在 ng-controller div 中。

在测试中,我得到了无法引用控制器的 user.password 的错误。控制器之前工作正常,我没有更改任何内容。对于这个问题,我发布了模态和控制器代码的简化版本。

$scope.login = function($event){ $event.preventDefault();
  // console.log("cond ", cond, ".checkbox.signup ", $scope.checkbox.signup);
  if (!$scope.logged)
  fn.login($scope.user, function(){
    if ($scope.checkbox.signup) fn.signup($scope.user);
  });
};
var fn = {
login: function(user, cb){
    if (Auth.authData) return;

    if (!user.password) {
      fn.alert("please type password");
      return;
    }
    if (fn.valid_email(user.email))
    Auth.ref_ds1.authWithPassword(user, function(error, authData) {
      if (error) {
        fn.alert(error);
        cb();
      } else {
        authData.email = $scope.user.email;
        console.log("Authenticated successfully on:", authData.email);
        fn.greet("Hello " + authData.email.split("@")[0]);
        $scope.logged = true;
        window.location.href = "/";
      }
    });
  }
}

如何正确引用它们?

【问题讨论】:

  • 错误读取 - TypeError: 无法读取未定义的属性“密码”......

标签: javascript html angularjs twitter-bootstrap angular-ui-router


【解决方案1】:

您的 ng-include 创建另一个范围时可能会遇到问题,并且您无法引用之前定义的用户对象。

避免与范围混淆的一种方法是使用“Controller as”语法来专门引用范围(请参阅http://www.johnpapa.net/angularjss-controller-as-and-the-vm-variable/)。这是一个小例子:

// in the controller
app.controller('MainController', function() {
    var vm = this;

    this.somevalue = 'something';
})

// markup
<div ng-controller="MainCtrl as ctrl">
  {{ ctrl.somevalue }}

  <div ng-controller="SecondCtrl as secondCtrl">
      {{ ctrl.somevalue }}
      {{ secondCtrl.anothervalue }}
  </div>
</div>    

使用“Controller as”确实有助于解决您遇到的范围问题,但需要对您的原始控制器进行一些重新设计。

【讨论】:

  • 是的,我检查了 html 元素并看到了模态 html 代码的嵌套范围。该本地范围被放置在环绕模式的 div 中。我无法引用父/根范围。您的回答帮助我确认了我对这个问题的想法,但重新制作工具确实让我感到害怕。除了 app.js 定义要附加的模块、配置和状态之外,我在 main.js 中的 MainCtrl 已经有大约 200 个 LoC,重新构建对我来说似乎是过度的工作。我的 main.js 包括 FirebaseUrl 常量、3 个工厂(1 个用于一组帮助函数、1 个 Auth、1 个 preload.get() 从 Firebase 获取数据)和 1 个 MainCtrl.
  • 您可以研究的一件事是创建一个保存用户数据的工厂,然后您可以将该工厂注入到 ng-include 中的父控制器和子控制器中,以便它们可以共享数据。
  • 好的,但我还没有为模态创建任何子控制器。不同的路由通过 app.js 中定义的 ui-route 得到不同的状态和控制器。事实上,如果我可以像注入工厂一样将一个控制器注入另一个控制器(比如将“ModalCtrl”注入“MainCtrl”),那么 IDK。我知道我的设计很糟糕(请启发我)。额外工厂的问题在于,我的应用程序允许不同的身份验证(Google、Github、电子邮件和密码)。来自 Google 等服务的 OAuth 不会返回用户名和密码以存储在该工厂中。对于该工厂,我们可能需要泛化,但泛化可能不起作用。
  • 在尝试中,我也将 ng-controller="MainCtrl" 附加到 md.html 文件中。它就像
猜你喜欢
  • 2012-02-07
  • 1970-01-01
  • 2018-01-29
  • 2019-03-04
  • 1970-01-01
  • 1970-01-01
  • 2017-12-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多