var ang = angular.module("myApp", []);
ang.controller("myController", function($scope) {
$scope.name = "Samar";
$scope.ageC = 26;
$scope.time = 20;
$scope.message = `Name will change in both Directive and Controller, Wait for `;
});
ang.directive("myDirective", function($timeout, $interval) {
return {
restrict: "EA",
scope: {
name: "=",
age: "@",
message: "=",
time: "="
},
link: function(scope, elem, att) {
//changing two way binding name
var intervalID = $interval(function() {
scope.time = scope.time - 1;
}, 1000)
$timeout(function() {
//att.age is found but i am not sure why att.name is pulling name as //a string. att.age and scope.age is pulling data correctly.
scope.name = scope.name + " Pattanayak";
scope.message = " 'Name' changed, now age will change but it will not reflect in Controller, Wait for ";
}, 10000)
//changing one way binding age
$timeout(function() {
$interval.cancel(intervalID);
scope.message = " Changes are done :-)";
scope.time = "";
//att.age is found but i am not sure why att.name is pulling name as //a string. att.age and scope.age is pulling data correctly.
scope.age = parseInt(att.age) + 50;
}, 20000)
},
template: '<div style="margin-left: 20px; margin-top:15px;color:#FF5833"><div >From Directive and age is <strong class="strongCSS"> {{age}} </strong>and his name is <strong class="strongCSS"> {{name}} </strong></div>Directive Name :<input type="text" class="textCSS" ng-model="name"/></br>Directive Age :<input type="text" class="textCSS" ng-model="age"/></div>'
}
});
ang.directive("errorDirective", function() {
return {
restrict: "EA",
require: "ngModel",
link: function(scope, elem, attr, ctr) {
var viewValue;
ctr.$parsers.push(function(inpVal) {
if (inpVal > 80) {
ctr.$setViewValue(viewValue);
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.