javascript - Give angular directive a model name to access it's scope in the parent -
i'm trying access isolated scope of directive using model attribute on it's element when use in html. eg.
<div controller="parent"> <hello-world data-ng-model="hw"/> <input type="submit"/> </div>
the controller parent:
function($scope){ $scope.submit = function(){ alert($scope.hw.t); } };
the directive 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 defined in isolated scope, because displayed correctly. however, when click submit button, error because hw undefined. (because hello-world scope isn't being assigned 'hw' scope variable of parent. how can let hw defined scope of hello-world directive? use case make date picker exposes date picked through it's scope. like
<date-picker ng-model="date1"/> <date-picker ng-model="date2"/>
in directive's scope, ensure month, year, etc. defined. in parent controller, can use $scope.date1.month
, similar access date picked.
following comments, seems need make custom validateable elements using ngmodel.
according ngmodel
's documentation, can done using manually ngmodelcontroller
. following page should contain information need (look @ script.js
in example): https://docs.angularjs.org/api/ng/type/ngmodel.ngmodelcontroller
good luck.
Comments
Post a Comment