Hi All,
I have found and modified following code for custom directives: Apple
directive dose not have it's own controller.
*directive.js*
var app = angular.module("MyApp", []);
app.controller('mainApp', ['$scope', function ($scope) {
$scope.counter = 0;
$scope.*counterUp* = function () {
$scope.counter += 1;
}
}]);
app.directive("basket", function () {
return {
restrict: "E",
controller: function ($scope, $element, $attrs) {
$scope.content = [];
this.addApple = function () {
$scope.content.push("apple");
};
this.addOrange = function () {
$scope.content.push("orange");
};
},
link: function (scope, element) {
element.bind("mouseenter", function () {
console.log(scope.content);
});
}
};
});
app.directive("apple", function () {
return {
require: "basket",
link: function (scope, element, attrs, basketCtrl) {
basketCtrl.addApple();
}
};
});
app.directive("orange", function () {
return {
require: ['basket', '^apple'],
link: function (scope, element, attrs, controllers) {
controllers[0].addOrange();
controllers[1].*counterUp*();
}
};
});
*HTML page*
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="MyApp">
<head>
<title></title>
</head>
<body ng-controller="mainApp">
<basket apple orange>Roll over me and check the console!</basket>
<script src="../angular.js"></script>
<script src="directive.js"></script>
</body>
</html>
As per the documentation
- ^ -- Look for the controller on parent elements, not just on the local
scope
- ? -- Don't raise an error if the controller isn't found
Still I am getting error like : *Error: [$compile:ctreq] Controller
'apple', required by directive 'orange', can't be found!*
But as per the documentation, "'^apple" should find the parent "mainApp"
controller !!!!. If I define a controller for apple directives, everything
works fine. So what is use of *^ *in this case?
Do guide.
Thanks,
Ketan Mehta
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.