简体   繁体   English

AngularJS ng-如果不在表单中工作

[英]AngularJS ng-if not working in a Form

I try to hide my form after it has been 'submitted'. 在“提交”之后我试图隐藏我的表单。 I want to do it with ng-if. 我想用ng-if来做。 Clicking on the button 'See' displays the form on and off, but it does not work on the button 'Add'. 单击“查看”按钮可以打开和关闭表单,但它不适用于“添加”按钮。 What is the reason for that? 这是什么原因?

I have created a JSFiddle. 我创建了一个JSFiddle。 Link to JSFiddle . 链接到JSFiddle

My Form: 我的表格:

  <form name="userAddForm" ng-submit="something()" class="form-horizontal" role="form">
    <div class="form-group">
      <label class="col-md-2 control-label">Username</label>

      <div class="col-md-3">
        <input name="username" ng-model="user.username" class="form-control" ng-required="true" placeholder="Username" type="text" />
      </div>
    </div>
    <div class="form-group">
      <label class="col-md-2 control-label">Password</label>

      <div class="col-md-3">
        <input name="password" ng-model="user.password" class="form-control" placeholder="Password" type="password" ng-required="true" />

      </div>
    </div>



    <button ng-disabled="userAddForm.$invalid" type="submit" ng-click="add=!add" class="btn btn-success">
      <span class="glyphicon glyphicon-ok"></span> Add
    </button>
  </form>
</div>

Every directive in angularjs create different level of scope.So,there is different level of scope, one at form tag and one at button inside the form which create parent and child relationship between form and button inside it.As add is,primitive variable,its changes in child is not reflected in parent scope. angularjs中的每个指令都创建了不同的范围级别。因此,有不同的范围级别,一个在表单标签处,一个在表单内部的按钮处,在其中创建表单和按钮之间的父子关系。作为add ,原始变量,它在子级中的更改不会反映在父级范围内。

So,either use $parent service or define thet add variable in following way. 因此,要么使用$parent服务,要么按照以下方式定义thet add变量。

$scope.objHidShow = {};
$scope.objHidShow.add= false;

Please, don't use $parent! 请不要使用$ parent!

Use proper prototypal inheritance. 使用适当的原型继承。

The reason it doesn't work is because you are creating a new scope through the usage of ngIf . 它不起作用的原因是因为你通过使用ngIf创建一个新的范围

It does not create an isolate scope so you don't have to worry about that . 所以你不必担心它不会创建一个分离的范围


When you pass a primitive , Javascript will "shadow" that value on the prototype of the new scope . 当您传递一个原语时 ,Javascript将在新范围的原型上“遮蔽”该值。

As such, it is no longer a direct reference to the value in your parent scope. 因此,它不再直接引用父范围中的值。

How do you get around that? 你怎么解决这个问题?

add a dot 添加一个点

That roughly translates into; 这粗略地转化为; use an object and bind to a property of that object. 使用对象并绑定到该对象的属性。 objects don't get shadowed as they are not a primitive. 对象不会被遮蔽,因为它们不是原始对象。 you get to keep the reference and it just works™ . 你得保留参考,它只是工作™

jsfiddle 的jsfiddle

i would do it by calling a controller function: 我会通过调用控制器函数来做到这一点:

$scope.something = function() {
  $scope.shown = !$scope.shown;
}

what i did is to define that function and i made your controller working for the complete form. 我所做的是定义该功能,我让你的控制器工作为完整的形式。

https://jsfiddle.net/udc2rjrn/2/ https://jsfiddle.net/udc2rjrn/2/

updated your fiddle. 更新你的小提琴。 merry christmas ;) 圣诞节快乐 ;)

The ng-if directive creates a new scope so you need to use $parent : ng-if指令创建一个新范围,因此您需要使用$parent

<button ng-disabled="userAddForm.$invalid" type="button" ng-click="$parent.add=!$parent.add" class="btn btn-success">

Updated JSFiddle 更新了JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM