当我单击“检查”按钮时,以下代码引发错误“ TypeError:无法读取未定义的属性’$ pristine’”。
app.controller('MainCtrl', function($scope) {
// other stuff
})
.controller('Ctrl2', function($scope) {
$scope.product = {description:'pump'};
$scope.output = 'unknown';
// uncomment to avoid undefined error, still can't see $pristine
// $scope.formHolder = {};
$scope.checkForm = function() {
$scope.descriptionTest = $scope.product.description;
if ($scope.formHolder.productForm.$pristine) {
$scope.output = 'yes';
}
if ($scope.formHolder.productForm.$dirty) {
$scope.output = 'no'
}
}
});
html
<body ng-controller="MainCtrl">
<div >
<ng-include ng-controller="Ctrl2" src="'myForm.html'"></ng-include>
</div>
</body>
myForm.html
<form name="productForm" novalidate>
<h2>myForm</h2>
description: <input type="text" name="description" ng-model="product.description"/>
<br>
<button ng-click="checkForm()">Check Form</button>
<br>
Form Pristine: {{output}}
<br><br>
I can see the description: {{descriptionTest}}
</form>
朋克
问题是我的Ctrl2看不到productForm。最初,我认为这与ng-include在创建子范围时所做的原型继承有关,因此我尝试在Ctrl2中添加变量:
$scope.productForm = {};
这摆脱了错误,但是我的控制器仍然无法正确看到$ pristine或$ dirty。
我终于通过在productForm上方添加$ scope.formHolder对象来使其工作:
朋克
.controller('Ctrl2', function($scope) {
$scope.product = {description:'pump'};
$scope.output = 'unknown';
// uncomment to avoid undefined error, still can't see $pristine
$scope.formHolder = {};
$scope.checkForm = function() {
$scope.descriptionTest = $scope.product.description;
if ($scope.formHolder.productForm.$pristine) {
$scope.output = 'yes';
}
if ($scope.formHolder.productForm.$dirty) {
$scope.output = 'no'
}
}
});
html
<form name="formHolder.productForm" novalidate>
为什么这样做? 还有更好的方法吗?
之所以以这种方式结束,是因为我有一个可以使用的表单和控制器/模板,并且希望在其他地方重用。我可能应该发出一条指令,但是除了表单的$ pristine和$
dirty功能外,其他所有功能都可以正常工作-所有ng-model vars均正确传递。
如何设置ng-include内包含的表单为prestine?答案是“违反所有规则”,但看起来更复杂。
当我编写表单控制器时,何时将$ pristine添加到作用域以及作用域?
编辑/回答:
我最初的问题可以归结为关于form指令如何写入范围的混乱。我的印象是,它将把事情带入
<form name="productForm">...
并为其添加属性,例如
$scope.productForm.$pristine = function() {...}
但是,它直接在productForm上面写:
$scope.productForm = formObject;
因此,表单对象存储在子对象中,而不是所选答案中说明的父对象中。
子范围继承的关键要素对我有所帮助,那就是在阅读而不是写作中要参考链条。因此,如果您设置类似于childScope.myThing.property=‘123’的内容,虽然看起来像是写入操作,但它首先必须进行读取才能找出myThing是什么。设置childScope.myThing=‘567’是直接写操作,完全不涉及查看父链。
要了解为什么解决方案formHolder
有效,您必须首先了解JavaScript原型链。让我们formHolder
用以下伪代码说明第一种情况:
$parentScope = {
//I'm a parent scope inside Ctrl2
productForm:{} //to avoid undefined reference error
}
$childScope = {
//I'm a child scope created by by ng-include
__protototype__: $parentScope
}
当form
指令被分析它产生FormController
被设置于$scope
下指示键属性name
的属性值。这几乎等同于:
$childScope.productForm = $formCtrl;
之后,这两个作用域如下所示:
$parentScope = {
//I'm a parent scope inside Ctrl2
productForm:{} //to avoid undefined reference error
}
$childScope = {
//I'm a child scope created by by ng-include
productForm: $formCtrl
__protototype__: $parentScope
}
因此,实际上您最终获得了两个包含不同对象的 不同范围的属性 。现在,在第二种情况下,您将遇到以下情况:
$parentScope = {
//I'm a parent scope inside Ctrl2
formHolder:{} //to avoid undefined reference error
}
$childScope = {
//I'm a child scope created by by ng-include
__protototype__: $parentScope
}
当form
指令这次设置FormController
实例$scope
时,它使用不同的 属性链 :
$childScope.formHolder.productForm = $formCtrl;
相当于写:
var formHolder = $childScope.formHolder; //since formHolder isn't defined on $childScope
//the JS runtime will look for it in the prototypes chain and find it inside $parentScope
//so here formHolder is the very same object you created and set on $parentScope
formHolder.productForm = $formCtrl;
希望它有助于理解第二种选择为何起作用。至于问题的第二部分-您的解决方案简单且完全可行-但是有几种其他处理方法,最好取决于实际使用情况:
问题内容: 和之间有什么区别?似乎你们两个都可以成为: 问题答案: 该班告诉你,形式已经被用户修改,而类告诉你的形式还没有被用户修改。因此,和是同一故事的两个方面。 这些类被设置在任意字段中,而形式具有两个属性,和。 您可以使用该函数将表单重置为原始状态(请注意,这是AngularJS 1.1.x功能)。 如果甚至在AngularJS的1.0.x分支中也想要-ish行为,则需要推出自己的解决方案。
问题内容: 我正在构建一个简单的angularjs应用,并且试图实现无需页面刷新的登录。 我在做什么 初始化时,ng-include会加载/ set / save。/ set / save 中有。因此,当单击指令时,将打开一个窗口,并且当关闭窗口时,应更改ng-include的src。 问题 当指令在ng-include内部使用时(ng-include src在init上具有默认值)时,什么都不会
问题内容: 什么是检测html加载结束的最佳方法?我想编写一些在加载完成后运行的代码。 问题答案: 有两种方法可以检测何时完成加载,具体取决于您的需要: 1)通过属性- 用于内联表达式。例如: 2)通过事件的是发射-为应用范围的处理。例如: 完成内容加载后
问题内容: 我正在尝试在内添加HTML代码段,但无法使用包含功能。看来的当前语法与以前的语法不同:我看到许多示例使用 但是在官方文档中,它说使用 但随后在页面下方显示为 无论如何,我尝试了 我的代码片段不是很多代码,但是有很多事情要做。这可能会引起问题,所以我只用单词代替了内容,仍然一无所获。 我还尝试过直接在页面中声明模板,如下所示: 并遍历了引用脚本的所有变体,仍然一无所获。 我的页面还有很多
我正在使用executorservice,每个webservice调用都会产生大约9-10个可调用任务,并提交给executorservice线程池。线程池大小为100的应用程序只有一个executorService。当我提交调用时,我有一个2 For循环。外部循环运行到指定的超时期满或完成的散列集大小==提交的任务大小;内部循环将遍历调用项,如果isDone()==true,则将这些调用项收集到
问题内容: 我知道Google搜索可以找到合适的答案,但是我更喜欢听您的个人(也许是技术性的)意见。 Java和C#之间在引发异常方面有所不同的主要原因是什么? 在Java中,引发异常的方法的签名必须使用“ throws”关键字,而在C#中,您不知道在编译时是否可以引发异常。 问题答案: 因为对已检查异常的响应几乎总是: 如果您确实知道如果抛出特定异常,您可以执行某些操作,那么您可以捕获该异常,然