[英]Access a form (for validation) from another a different AngularJS component
我有兩個組成部分。 一個在其中有一個表單,另一個在其中有導航,當單擊導航按鈕時,我需要訪問該表單以確保它有效,然后再繼續下一步。 但是,當我嘗試從另一個組件訪問它時,該窗體始終是未定義的。 是否可以從另一個組件訪問表單,如果可以,我該如何完成?
的index.html
<!DOCTYPE html>
<html>
<head>
<script data-require="angularjs@1.5.8" data-semver="1.5.8" src="https://opensource.keycdn.com/angularjs/1.5.8/angular.min.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body ng-app="app">
<form-component></form-component>
<nav-component></nav-component>
</body>
</html>
的script.js
// Code goes here
function checkForm(form) {
if (form) {
if (form.$valid) {
alert("Form is valid!");
}
else
{
alert("Form is invalid!");
}
}
else {
alert("FAILED! Form is undefined!");
}
}
function formController() {
var model = this;
model.goToNextStep = function(form) {
model.form = form;
checkForm(form);
};
}
function navController() {
var model = this;
model.goToNextStep = function(form) {
model.form = form;
checkForm(form);
};
}
var app = angular.module("app", []);
app.component("formComponent", {
template: `
<div class="container">
<form name="mainForm">
<p>When you click the button that exists in the same component
as the form, everything works fine.\
</p>
<input type="text" name="test123" value="THIS IS A TEST" />
<button type="button" ng-click="model.goToNextStep(mainForm);">
Submit Form
</button>
</form>
<br /><strong>formComponent model:</strong> <pre>{{model | json }}</pre>
</div>
`,
controllerAs: "model",
controller: [formController]
});
app.component("navComponent", {
template: `
<div class="container">
<p>When you click the button that does NOT exist in the same
component as the form, it doesn't work.
</p>
<button type="button" ng-click="model.goToNextStep(mainForm);">Submit Form</button>
<br /><br /><strong>navComponent model:</strong>
<pre>{{model | json }}</pre>
</div>
`,
controllerAs: "model",
controller: [navController]
});
一種方法是將兩個組件嵌套在ng-form元素中 :
<ng-form name="top">
<form-component top-form="top"></form-component>
<nav-component top-form="top"></nav-component>
</ng-form>
並使用單向<
綁定將兩者連接到頂部表單:
app.component("formComponent", {
bindings: {topForm: "<"},
template: `
<div class="container">
<form name="mainForm">
<p>When you click the button that exists in the same component as the form, everything works fine.</p>
<input type="text" name="test123" value="THIS IS A TEST" />
<button type="button" ng-click="model.goToNextStep(model.topForm);">Submit Form</button>
</form>
<br /><strong>formComponent model:</strong> <pre>{{model | json }}</pre>
</div>
`,
controllerAs: "model",
controller: [formController]
});
app.component("navComponent", {
bindings: {topForm: '<'},
template: `
<div class="container">
<p>When you click the button that does NOT exist in the same component as the form, it doesn't work.</p>
<button type="button" ng-click="model.goToNextStep(model.topForm);">Submit Form</button>
<br /><br /><strong>navComponent model:</strong> <pre>{{model | json }}</pre>
</div>
`,
controllerAs: "model",
controller: [navController]
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.