I create a dateRangePicker
directive that I declare like this:
<date-range-picker start="schedule.start" end="schedule.end"></date-range-picker>
My directive is declared as the following:
.directive("dateRangePicker", function () {
return {
restrict: 'E',
require: '^form',
scope: {
start: '=',
end: '='
},
replace: true,
templateUrl: 'dateRangePicker.tpl.html',
link: function (scope, elm, attrs, ctrl) {
The issue is that my template: dateRangePicker.tpl.html
initializes a child scope (the angular-ui's DatePicker component does it through its corresponding controller), so any attempt to write values to scope.start
or scope.end
would not affect the directive scope.
I would like to declare my isolated scope, so that the conflict won't occur:scope.schedule.start
and scope.schedule.end
.
I tried to do this:
return {
restrict: 'E',
require: '^form',
scope: {
schedule: {
start: '=start',
end: '=end'
}
}
But this error occurs:Error: 'undefined' is not a function (evaluating 'definition.match(LOCAL_REGEXP)')
Is there a way to achieve it using isolated scope?
I just don't want to have to propagate the scopes through scope:false
(that works, I tested it) in order to keep a good encapsulation.
Isolated Scope In AngularJS. It can access its parent scope through the $parent property. So, Directive has three options for isolating its scope from parent scope. The following are the three options: scope: false - It is default in Directive. It lets to reuse the scope from the place where the component is being used.
Here's the explanation: An Isolated scope property can be bind with DOM attributes. Interpolate or attribute sets up a one-way data binding from the Parent scope to the Isolated Scope of Directive. It means if Parent scope does any changes then changes will be reflected to the Isolated scope of Directive.
Isolated scope completely decouples component or template from the rest of the application or a place where it is being used. The following is the figure related to Isolated scope concept: There are three types of interface to specify between the element's attributes and the isolated scope:
Isolating the scope in a directive is a simple process. Start by adding a scope property into the directive, as shown next. It automatically isolates the directive’s scope from any parent scope (s). The @ local scope property is used to access string values that are defined outside the directive.
You can pass complex object to scope but not using the syntax you have used. Your parent scope should have schedule object and then in html you can do
<date-range-picker schedule="schedule"></date-range-picker>
Now you can use it in isolated scope using the familiar syntax
scope :{
schedule:'='
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With