Angularjs two way data binding with directive isolate scope binding example
In this post we will give you information about Angularjs two way data binding with directive isolate scope binding example. Hear we will give you detail about Angularjs two way data binding with directive isolate scope binding exampleAnd how to use it also give you demo for it if it is necessary.
Two way data binding means if changes in one then it reflects in other one too immediately and in Angularjs two way data binding between model and the view is happening by ng-model directive.
Its the synchronization between the model and the view.
When any changes in model then view reflects those changes and accordingly if any changes made in view then model gets updated as well and the main thing is it that this process does not take time.
Here you will find a simple example which make you understand the concept of two way data binding in Angularjs Example.
- <div ng-app="sampleapp" ng-controller="mycontroller">
- Product Name:<input ng-model="productname">
- <h3>{{productname}}</h3>
- </div>
- <script>
- var app = angular.module('sampleapp',[]);
- app.controller('mycontroller',function($scope){
- $scope.productname ="onlinecode";
- });
- </script>
<div ng-app="sampleapp" ng-controller="mycontroller">
Product Name: <input ng-model="productname">
<h3>{{productname}}</h3>
</div>
<script>
var app = angular.module('sampleapp', []);
app.controller('mycontroller', function($scope) {
$scope.productname = "onlinecode";
});
</script>
Double braces {{ }} are used to display content from model. Whatever you will type in input field then model data will change automatically.
Two way data binding in angularjs is a best way to show response of user’s action immediately.
ng-app is a root element of angularjs and every angularjs application must have ng-app directive.
Isolated scope in directive angularjs
Two way data binding are prefix with = or =attr
First i will tell you to create custom directive.
.directive function is used to create new directive. Always use camel case name myDirective when naming a directive and when creating html element for directive use - separated name such as my-directive. It is best practice to use directive via tag name.
- <my-directive></my-directive>
<my-directive></my-directive>
Directive with attribute :
- <my-directiveproduct="myProduct"></my-directive>
<my-directive product="myProduct"></my-directive>
Now i am going to use = local scope property for two way data binding.
Controller :
- $scope.myProduct ={name:'onlinecode'};
$scope.myProduct = { name:'onlinecode'};
Controller defines a $scope.myProduct object.
Directive :
- angular.module('myModule').directive('myDirective',function(){
- return{
- scope:{
- product:'='//Two-way data binding
- },
- template:'<ul><li ng-repeat="p in product">{{ p }}</li></ul>'
- };
- });
angular.module('myModule').directive('myDirective', function () {
return {
scope: {
product: '=' //Two-way data binding
},
template: '<ul><li ng-repeat="p in product">{{ p }}</li></ul>'
};
});
In directive i use ng-repeat to iterate over product list and = character inform to directive that object which are passing into the product property should be bound using two way binding and if any changes are made in outside property then product property should be automatically changed and if any changes are made into directive’s product property then object in outside scope should be automatically updated as well.
Now you can pass data into directive.
Label :
Angular JS
Hope this code and post will helped you for implement Angularjs two way data binding with directive isolate scope binding example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs
For More Info See :: laravel And github
PHP - I Can't get the $_POST Values on Ajax Request
In this post we will give you information about PHP - I Can't get the $_POST Values on Ajax Request. Hear we will give you detail about PHP - I Can't get the $_POST Values on Ajax RequestAnd how to use it also give you demo for it if it is necessary.
I want to share this posts with you because when i was working on native PHP and Ajax Post request(Specially AngularJS Post request) at that time i can't get Post value on form submit. That's why i would like to share you this post. I also want to tell you i was working on my Ubuntu 14.04. I did try lot but i can't get value.
At last find stuff to how to solve this issue, i did use "file_get_contents('php://input')" that way i could get POST value in json object and i also decode json value using "json_decode()". So let's try this way :
Example:
$post = file_get_contents('php://input');
$post = json_decode($post);
$sql = "INSERT INTO items (title) VALUES ('".$post->title."')";
$result = $mysqli->query($sql);
Hope this code and post will helped you for implement PHP - I Can't get the $_POST Values on Ajax Request. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs