I'm trying to access the isolated scope of a directive by using the model attribute on it's element when I use it in the HTML. Eg.
<div controller="parent">
<hello-world data-ng-model="hw"/>
<input type="submit"/>
</div>
The controller for the parent:
function($scope){
$scope.submit = function(){
alert($scope.hw.t);
}
};
The directive for the hello-world:
app.directive('helloWorld', function() {
return {
link: function(scope, element, attrs){
scope.t = 'test';
},
replace: true,
restrict: 'E',
scope: {},
template: '<h5>Hello world {{t}}</h4>'
};
});
t is defined in the isolated scope, because it is displayed correctly. However, when I click the submit button, I get an error because hw is undefined. (Because the hello-world scope isn't being assigned to the 'hw' scope variable of the parent. How can I let hw be defined as the scope of the hello-world directive? My use case is to make a date picker that exposes the date picked through it's scope. Like
<date-picker ng-model="date1"/>
<date-picker ng-model="date2"/>
In the directive's scope, I would ensure that the month, year, etc. are defined. Then in the parent controller, I can use $scope.date1.month
and similar to access the date picked.