I'm learning angular and when I was studying custom directives I got stuck in a situation that I don't know how to solve. Here are my custom direcives:
/**
* Created by Lucas on 11/06/2015.
*/
'use strict'
eventsApp
.directive('greeting',function() {
return {
restrict: 'E',
replace: true,
template: "<button class='btn' ng-click='sayHello()'>Say Hello</button>",
controller: function GreetingController($scope) {
var greetings = ['hello'];
$scope.sayHello = function() {
alert(greetings.join());
}
this.addGreeting = function(greeting) {
greetings.push(greeting);
}
}
};
})
.directive('finnish',function() {
return {
restrict: 'A',
require: 'greeting',
link: function(scope, element, attrs, controller) {
controller.addGreeting('hei');
}
}
})
.directive('hindi',function() {
return {
restrict: 'A',
require: 'greeting',
link: function(scope, element, attrs, controller) {
controller.addGreeting('नमस्ते');
}
}
});
These directives work fine, however, if I change the greeting controller to receive an external controller via an attribute like this:
.directive('greeting',function() {
return {
restrict: 'E',
replace: true,
template: "<button class='btn' ng-click='sayHello()'>Say Hello</button>",
controller: '#',
name: 'ctrl'
};
})
And make a reference in the HTML like this:
<!DOCTYPE html>
<html ng-app="eventsApp">
<head lang="en">
<meta charset="UTF-8">
<title>Event Registration</title>
<link rel="stylesheet" href="css/bootstrap.min.css"/>
<link rel="stylesheet" href="css/app.css"/>
</head>
<body>
<greeting ctrl="GreetingController">
<div finnish hindi></div>
</greeting>
<script src="lib/jquery.min.js"></script>
<script src="lib/angular/angular.js"></script>
<script src="lib/angular/angular-resource.js"></script>
<script src="lib/angular/angular-route.min.js"></script>
<script src="lib/underscore-1.4.4.min.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers/GreetingController.js"></script>
<script src="js/directives/greeting.js"></script>
<script src="js/filters.js"></script>
<script src="lib/bootstrap.min.js"></script>
</body>
</html>
The other directives stop working, angular logs the following error:
Error: [$compile:ctreq] Controller 'greeting', required by directive 'hindi', can't be found!
I imagine that there is a way to fix this, but since I'm new in Angular I'm quite a bit lost.
If you are getting the controller as an attribute, the attribute name needs to be the name of the directive:
<greeting greeting="GreetingController" finnish="" hindi=""></greeting>
See this plunker
Related
var erroMessage = "<br/>For<br/><a ng-click='RR.hiphen();'><strong>Visit</strong></a>");
$scope.Error = {
alertType:"error",
title: "",
message: GlobalObjectErrorMessage + erroMessage,
};
I am getting a simple without the ng-click like below:
<a><strong>Visit</strong></a>
You have to compile it using $compile because it is not a part of dom.
<dir content="{{ erroMessage }} ">
errorMessage variable should be part of $scope so it can be access in html view,then add directive to your controller or any where else to convert content to html by compiling it.
.directive('dir', function ($compile, $parse) {
return {
restrict: 'E',
link: function (scope, element, attr) {
scope.$watch(attr.content, function () {
element.html($parse(attr.content)(scope));
$compile(element.contents())(scope);
}, true);
}
}
})
As i dont have your complete js code i arrange with my data and put an alert in click of that function call
<!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 data-require="angular.js#1.4.x" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<div dynamic="erroMessage "></div>
</body>
<script>
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.erroMessage = "<br/>For<br/><a ng-click='hey();'><strong>Visit</strong></a>";
$scope.Error = {
alertType:"error",
title: "",
message: "hey" + $scope.erroMessage ,
};
$scope.hey=function(){
alert("i am coming from your vist click");
}
});
app.directive('dynamic', function ($compile) {
return {
restrict: 'A',
replace: true,
link: function (scope, ele, attrs) {
scope.$watch(attrs.dynamic, function(erroMessage ) {
ele.html(erroMessage );
$compile(ele.contents())(scope);
});
}
};
});
</script>
</html>
I am trying write an angular directive to make a substring of an attribute that is passed in. Below is my code:
HTML:
<body ng-controller="MainCtrl">
<div><substring message="This is a test."></substring></div>
<div><substring message="So is this." ></substring></div>
</body>
Angular/Javascript:
var app = angular.module('myapp', []);
app.controller('MainCtrl', function($scope) {
});
app.directive('substring', function () {
return {
restrict: 'AE',
replace: true,
scope: { text: '=message' },
link: function (scope, elem, attrs) {
//alert(attrs.message);
var str = attrs.message;
scope.text = str.substring(1, 4);
},
template: '<H1>{{text}}</H1>'
};
});
When I try running this I get the following error:
HTML1300: Navigation occurred.
File: directive.html
Error: [$parse:syntax] Syntax Error: Token 'is' is an unexpected token at column 6 of the expression [This is a test.] starting at [is a test.].
Also, I have tried changing
'=message' to '#message'
but that just causes the substring stuff I am doing in the link function to get ignored.
Why the error? Is Angular not seeing that stuff in the quotation marks as a string and instead trying to parse out some command? Most importantly, how do I fix this?
Thanks
Simply use # for text binding and the rest of your code works perfectly.
Working example:
var app = angular.module('myapp', []);
app.controller('MainCtrl', function($scope) {
});
app.directive('substring', function() {
return {
restrict: 'AE',
replace: true,
scope: {
message: '#'
},
link: function(scope, elem, attrs) {
//alert(attrs.message);
var str = attrs.message;
scope.text = str.substring(1, 4);
},
template: '<H1>{{text}}</H1>'
};
});
<!DOCTYPE html>
<html ng-app="myapp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body ng-controller="MainCtrl">
<div>
<substring message="This is a test."></substring>
</div>
<div>
<substring message="So is this."></substring>
</div>
</body>
</html>
Considering a directive having an isolated scope (object), and restrict A, How can I pass their attributes?
For instance, when it comes to restrict E, if scope equals to {attr : '#'} , then the directive will be called like .
The attributes can be passed in same way as we pass for E type directives.
Refer: http://plnkr.co/edit/T2R91F0iR9GfttSav9Zq?p=preview
//HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-example12-production</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body ng-app="docsSimpleDirective">
<div ng-controller="Controller">
<div my-customer customer="customer" testv="Hello"></div>
</div>
</body>
</html>
//JS
(function(angular) {
'use strict';
angular.module('docsSimpleDirective', [])
.controller('Controller', ['$scope', function($scope) {
$scope.customer = {
name: 'Naomi',
address: '1600 Amphitheatre'
};
}])
.directive('myCustomer', function() {
return {
restrict: 'A',
scope: {
customer:'=',
testv: '#'
},
template: 'Name: {{customer.name}} Address: {{customer.address}} - {{v1}}',
link: function(scope, element, attrs) {
console.log(attrs);
scope.v1=attrs.testv;
}
};
});
})(window.angular);
When we create directive with restrict 'E' which stands for 'Element', then it will call like as -
<my-attr-directive attr="somevalue" ></my-attr-directive>
the same like with restrict E:
<div my-attr-directive attr="somevalue"></div>
I have a parent directive in which its controller makes a call through a service to get some data.
sharedService.getData(options).then(function(data){
$scope.data = data;
});
Now i need this data in my child controller.
What i have already tried are the ff:
1) Through $timeout i get the data after sometime but it doesn't seem a good solution impacting performance
2) watchCollection() - i watched if newValue !== oldValue
problem being the data is huge so it takes a toll of performance
Now the issue i'm getting is the child directive gets executed after parent BUT before the data comes back from the service and i'm not able to get that data in my child directive via $scope.data.
Is there any solution to get data from parent directive to child directive when i have to wait for data to come in parent?
You can include your parent directive controller in your child directive by using require.
angular.module('myApp', [])
.directive('dirParent', function() {
return {
restrict: 'E',
scope: {},
controller: ['$scope', function($scope) {
}],
};
})
.directive('dirChild', function() {
return {
require: '^dirParent', // include directive controller
restrict: 'E',
scope: {
},
link: function(scope, element, attrs, paretCtrl) {
var data = paretCtrl.getMyData();
}
};
})
It's always a best to use service for communication and and business logic. Here is an example. Please check. This might solve your problem.
// Code goes here
angular.module('app', [])
.factory('messageService', function() {
return {
message: null
}
})
.directive('parentDir', function() {
return {
scope: {}, //isolate
template: '<input type="text" ng-model="PDirInput"/><button ng-click="send()">Send</button>',
controller: function($scope, messageService) {
$scope.send = function() {
messageService.message = $scope.PDirInput;
}
}
}
})
.directive('childDir', function() {
return {
scope: {}, //isolate
template: '<code>{{CDirInput.message}}</code>',
controller: function($scope, messageService) {
$scope.CDirInput = messageService;
}
}
})
<!DOCTYPE html>
<html ng-app="app">
<head>
<script data-require="angular.js#*" data-semver="2.0.0-alpha.31" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body>
<HR/>Parent Directive
<parent-dir></parent-dir>
<br/>
<HR/>Child Directive
<child-dir></child-dir>
<HR/>
</body>
</html>
edit: Modified the code as per stevuu's suggestion as well as added a plunkr to here
I'm currently attempting to have a child directive call a method(resolve) through another directive all the way up to a parent directive but I'm having difficulties identifying the problem with my approach.
The problem right now seems to be that although resolve() does get called as expected on click, selected remains undefined.
the html:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Angular: directive using & - jsFiddle demo</title>
<script type='text/javascript' src='//code.jquery.com/jquery-1.9.1.js'></script>
<link rel="stylesheet" type="text/css" href="/css/normalize.css">
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<script type='text/javascript' src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<style type='text/css'>
</style>
</head>
<body ng-app="myApp">
<div grand-parent>
<span>selected: {{text}}</span>
<div parent resolve="resolve"></div>
</div>
</body>
</html>
And the js:
var myApp = angular.module('myApp',[]);
myApp.directive('grandParent', function() {
return {
scope:{
resolve: "&"
},
link: function($scope, $element) {
$scope.resolve = function(selected){
$scope.text = selected
}
}
};
});
myApp.directive('parent', function(){
return{
scope: {
resolve: "&"
},
template: "<div child resolve='resolve'></div>"
};
});
myApp.directive('child', function() {
return {
scope: {
resolve: "&"
},
template: "<div>Click me!</div>",
link: function($scope, $element) {
$element.on("click", function(){
$scope.$apply(function(){
$scope.resolve({selected: "Yahoo!!"});
});
});
}
};
});
resolve: "&" is a mapping. So here:
myApp.directive('grandParent', function() {
return {
scope:{
resolve: "&"
},
link: function($scope, $element) {
$scope.resolve = function(selected){
$scope.text = selected
}
}
};
});
you are trying to map "resolve" to ... nothing, because "grandParent" doesn't have any attr named "resolve".
If you want to share some staff betweens directives you should do something like that:
view
<div data-grand-parent resolve="resolved()">
<div data-parent ></div>
</div>
Directives
var app = angular.module('test');
app.directive('grandParent', function() {
return {
scope : {
resolve : "&" // in view we defined resolve attr
// with "resolve()" value, so now resolve=resolved()
// in grandParent scope too.
},
controller: function($scope){
this.getResolve = function(){
return $scope.resolve;
};
}
};
});
app.directive('parent', function() {
return {
require: "^grandParent",
link: function(scope, element, attr, grandParentCtrl){
grandParentCtrl.getResolve()();
},
template : ""
};
});
controller
angular.module('desktop')
.controller('MainCtrl', function ($scope, mocks) {
$scope.resolved = function(){
console.log("calling $scope.resolved() ...");
};
});
output
calling $scope.resolved() ...
So, how does it work?
We defined resolved function in our controller, then we sign this function to attr "resolve" in grandParent directive. Thx to resolve : "&" we could mapped that resolved() function to "resolve" property in grandParent scope. At the end we inject grandParent to other directives. That's all.
I recommend you to read angularJs by Brad Green, Shyam Seshadri. it's not the best book but could be worse and it's free. You can find very good tutorial too on http://www.egghead.io/
ps. Sorry for my english ;/