Require a directive input box text in angular - javascript

I have a custom directive with an input box. This input is used in many of the pages. I want to require this input field in some of the forms and exclude in others. What should be the best approach to do this?
I am submitting the form from controllers so not sure how this would work in angular?
I am new to angular and not sure how this all will be tie up together.
So essentially this is what I want :
<form name="myForm" >
<div ng-contoller="Somecontroller as vm" >
<custom-directive>
this will create an input box like this :
<input type="text"> </input>
</custom-directive>
</div>
</form>
When I submit the form I want to ensure that if the text box inside the directive does not have text then I should get an error.

Hi, this is your custom directive, with this example you can handle what you want...also you can use directive in all you form but remember do not use with same ngModel, and last one thing always set ngModel in this directive.
var app = angular.module("app", []);
app.directive("requiredInput", function () {
return {
restrict: "E",
required: "^ngModel",
template: "<input type='text' ng-model='ngModel' ng-required='true'>",
scope: {
ngModel: "="
}
}
})
<!DOCTYPE html>
<html ng-app="app">
<head>
<title></title>
</head>
<body>
<h1>form 1</h1>
<form name="form">
<required-input ng-model="form.myInput"></required-input>
<required-input ng-model="form.myInput2"></required-input>
{{myInput}}
<button ng-disabled="form.$invalid">submit</button>
</form>
<h1>form 2</h1>
<form name="form2">
<required-input ng-model="form2.myInput"></required-input>
{{myInput2}}
<button ng-disabled="form2.$invalid">submit</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</body>
</html>

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body ng-app="myApp">
<w-test-directive></w-test-directive>
<script>
var app = angular.module("myApp", []);
app.directive("wTestDirective", function() {
return {
template : "<input type='text'/>"
};
});
</script>
</body>
</html>

Related

Why won't this Angular ng-bind tie to an ng-controller?

I'm learning Angular.js. I want to make a form where the user can see the output has they fill it out.
Here's test.html:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0-rc.2/angular.js"></script>
<script src="test.js"></script>
</head>
<body ng-app="charter">
<div id="layout-wrapper" ng-controller="graphicLanguageCTRL">
<form id="graphic-language" ng-controller="graphicLanguageCTRL" novalidate>
<h3>Language</h3>
<input type="text" name="headline" placeholder="Headline" ng-model="graphic.hed"> <br>
</form>
</div>
<h1 class="graphic-title" ng-bind="graphic.hed"></h1>
</body>
Here's test.js:
var app= angular.module("charter",[]);
app.controller("graphicLanguageCTRL",function($scope){
$scope.master= {
hed: ''
};
});
I want the stuff typed into the input tag to be visible in the h1 tag. But when I type into the input tag, nothing appears in the h1 tag.
How do I fix this?
There are some errors in your code:
You are using ng-bind outside of controller
You are initializing graphicLanguageCTRL twice
In your script you are setting the wrong variable name
Below is the fixed code:
html
<body ng-app="charter">
<div id="layout-wrapper" ng-controller="graphicLanguageCTRL">
<form id="graphic-language" novalidate>
<h3>Language</h3>
<input type="text" name="headline" placeholder="Headline" ng-model="graphic.hed"> <br>
</form>
<h1 class="graphic-title" ng-bind="graphic.hed"></h1>
</div>
</body>
JS
var app = angular.module("charter",[]);
app.controller("graphicLanguageCTRL", function($scope){
$scope.graphic = {
hed : ''
};
});
var app = angular.module("charter",[]);
app.controller("graphicLanguageCTRL",function($scope){
$scope.graphic = {
hed: ''
};
});
or
var app = angular.module("charter",[]);
app.controller("graphicLanguageCTRL",function($scope){
$scope.graphic = {};
$scope.graphic.head = ''
});
2 things need to be taken care of:
Do not nest and use the same ng-controller
Move the h1 inside
the ng-controller block
Fixed version:
var app = angular.module('charter', []);
app.controller("graphicLanguageCTRL", function($scope) {
$scope.graphic = {
hed: ''
};
});
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0-rc.2/angular.js"></script>
<meta charset="utf-8">
<title>test</title>
</head>
<body ng-app='charter'>
<div id="layout-wrapper" ng-controller="graphicLanguageCTRL">
<form id="graphic-language" novalidate>
<h3>Language</h3>
<input type="text" name="headline" placeholder="Headline" ng-model="graphic.hed">
<br>
</form>
<h1 class="graphic-title" ng-bind="graphic.hed"></h1>
</div>
</body>
</html>

Hide the element if the length and ng-pattern not satisfied angular

I only need to show the span if the user input value is greater than 1 and the input field doesn't contains the values defined in the regular expression.
Code:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-ng-form-production</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-animate.js"></script>
<script type="text/javascript">
angular.element(document.getElementsByTagName('head')).append(angular.element('<base href="' + window.location.pathname + '" />'));
</script>
</head>
<body ng-app="formExample">
<script>
angular.module('formExample', [])
.controller('FormController', ['$scope', function($scope) {
$scope.userType = '';
$scope.regex = "#%<;'=";
}]);
</script>
<form name="myForm" ng-controller="FormController" class="my-form">
userType:
<input name="input" ng-model="userType" ng-pattern="regex && userType">
<span ng-show="userType && myForm.input.$valid">show me!</span>
<br>
</form>
</body>
</html>
Plunker Demo
If you need a entry without your regex pattern, you need a exclusion mask:
$scope.regex = "[^#%<;'=]*";
And use a not conditional mask on pattern:
<input name="input" ng-model="userType" ng-pattern="regex">
plunker here
ng-pattern only validates regular expression. So inside ng-pattern there should be just pattern. At the time of showing message check the userType and the other form property inside ng-show or ng-hide. That's all.
<form name="myForm" ng-controller="FormController" class="my-form">
userType:
<input name="input" ng-model="userType" ng-pattern="regex">
<span ng-show="userType && !myForm.input.$error.pattern">show me!</span>
<br>

Is it possible to apply one expression for multiple directives?

Here is a simple code, where I tried to implement what I'm talking about:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-example32-production</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-beta.0/angular.min.js"></script>
</head>
<body ng-app="app">
<div ng-controller="AppController">
<form name='AlertForm' ng-init="action = 'AlertForm.$valid && print(text)'">
Input: <input type="text" ng-model="text" ng-keyup='$event.keyCode == 13 && {{action}}' /><br />
<input type="button" ng-click="{{ action }}" value="Alert" />
</form>
</div>
<script>
angular.module('app', [])
.controller('AppController', ['$scope', function($scope) {
$scope.print = function(msg) {
alert(msg);
};
}]);
</script>
</body>
</html>
http://plnkr.co/edit/M7hfHytCzqrOMMLSpbZm?p=preview
There is a form with a single input and a button. If you press the button or press enter, while staying in the input, it will alert you with content of the input.
I've tried to use ng-init and Angular templating for caching the print(text) expression, to avoid redundancy, but it doesn't work.
Is there any other ways, to do that without touching controller?

Pass hidden values to JSON using AngularJS

Im basic developer , just wanted to pass hidden values from my html page in a json array . Following is my code :-
Index.html
<!doctype html>
<html ng-app="plunker" >
<head>
<meta charset="utf-8">
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');
</script>
<link rel="stylesheet" href="style.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.js"></script>
<script src="app.js"></script>
</head>
<body>
<div ng-controller="WorkingCtrl">
<h1 ng-bind="vm.heading"></h1>
<form name="myForm" novalidate>
<b> Text: </b>
<input ng-model="form.text" required /><br>
<button ng-click="submitForm()" bng-disabled="myForm.$invalid">Click Me!</button>
</form>
<p ng-bind="showValues"></p>
</div>
</body>
</html>
app.js
var app = angular.module('myApp');
app.controller('WorkingCtrl', ['$scope', '$http', function ($scope, $http) {
$scope.form = {};
$scope.submitForm = function () {
$scope.showValues = JSON.stringify($scope.form);
}
}]);
Now , the value from ng-model="form.text" successfully returns the result as for unhidden input types
{ "text":"What_ever_text" }
but how to send a defined value in array, consisting of hidden input type like :-
<input value="99999" type="hidden" ng-model="form.userid" required />
So , wanted desired output like :-
{ "text":"What_ever_text" , "userid":"99999" }
any solution ?
Note :- The input type should be hidden or any other easier method appreciated. Thanks in advance.
hidden input fields also works same as normal fields only.
see the below code
var app = angular.module('myApp', []);
app.controller('WorkingCtrl', ['$scope', '$http', function ($scope, $http) {
$scope.form = {
text: "some test",
userid: '12312312312'
};
$scope.submitForm = function () {
$scope.showValues = JSON.stringify($scope.form);
}
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="WorkingCtrl">
<h1 ng-bind="vm.heading"></h1>
<form name="myForm" ng-submit="submitForm()" novalidate>Text:
<input ng-model="form.text" required />
<br/>
<input type="hidden" ng-model="form.userid" required />
<button bng-disabled="myForm.$invalid">Click Me!</button>
</form>
<p ng-bind="showValues"></p>
</div>
</div>

Unable to access form of ng-include file $scope

When I inspect $scope I see firstForm but I don't see secondForm in the object. I am at a loss as to why this is happening.
Here's the sandbox code:
index.html
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
<script>
var firstController = function ($scope){ };
</script>
</head>
<body>
<div ng-controller="firstController">
<form name="firstForm"></form>
<div ng-include="'second_form.html'"></div>
</div>
</body>
</html>
second_form.html
<form name="secondForm"></form>
Thanks to #charlietfl for this suggestion:
Might consider a simple directive to switch with ng-include that just has templatetUrl then won't have that child scope
index.html
<html ng-app="myModule">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
<script>
var myModule = angular.module('myModule', []);
myModule.directive('secondForm', function () {
return{
templateUrl: 'secondform.html'
}
});
var firstController = function ($scope){
console.log($scope);
};
</script>
</head>
<body>
<div ng-controller="firstController">
<form name="firstForm">
</form>
<div second-form></div>
</div>
</body>
This works!
ng-include will create another $scope. If you want firstController() to handle both forms the do the following:
<div>
<form ng-controller="firstController" name="firstForm"></form>
<div ng-include="'second_form.html'"></div>
</div>
Then in second_form.html do:
<form ng-controller="firstController" name="secondForm"></form>
use other controllers in for ng-include directives.
In secondForm.html specify controller name as
<div ng-controller="secondCtrl">
</div>
So wen you use ng-inculde inside main controller all controllers of the subtemplates inside ng-include will inherit from the main controller,i.e it will have parent-child relationsship..
More more info go to this link
ng-include and ngRoute: how to make them work together? (i.e. route to a view wihin a ng-include)

Categories

Resources