AngularJS: access directive data from controller -
lets assume have angularjs directive looking this:
app.directive('psdir', [ function() { return { template: "<div style='padding: 5px; border: 1px solid red; margin-bottom: 10px;'><p>this direcive:</p> <textarea rows='5' cols='50' ng-model='md'></textarea></div>", restrict: 'aec', scope: {} } } ]);
i using directive number of times on single page. how value of every directive instance/scope of ng-model="md"
in mainctrl
(i.e. want save value in add()
) :
app.controller('mainctrl', ['$scope', function($scope) { console.log("init"); $scope.add = function() { console.log($scope); } } ]);
plunker demo: http://embed.plnkr.co/q5bw6cbxpyene7q6vpsk/preview
any suggestions appreciated.
since creating isolated scope , otherwise cannot access child scope parent scope.
the way out pass model parameter parent like
<div class="psdir" model='field2'></div> <div class="psdir" model='field1'></div>
then in directive update them attribute binding. see update plunkr
Comments
Post a Comment