Sunday, 15 May 2011

angularjs - Data binding not working in an event in directive -



angularjs - Data binding not working in an event in directive -

i cannot info binding work in directive. variables not bound when alter them within event handler,

what doing wrong? > test fiddle

var myapp = angular.module('myapp', []) .directive('inputtest', function () { homecoming { restrict: 'e', template: '<div class="input-group">\ <input type="text" class="form-control" />\ <br>child scope: {{myvalue}}\ </div>', scope: { myvalue: '=', }, link: function (scope, element, attrs) { $(element).on('click', function (e) { alert('c'); scope.myvalue = 'clicked'; }); scope.myvalue = 'not clicked'; }, }; }) function myctrl($scope) { $scope.myvalue = 'parent value'; }

html

<div ng-controller="myctrl">parent scope: {{myvalue}} <input-test my-value="myvalue"></input-test> </div>

do not forget phone call $scope.$apply() @ end of event handler.

first level bindings may not work expected due how prototypical inheritance works. if seek first point , still no results, seek putting myvalue level deeper:

$scope.data.myvalue = 'parent value';

and:

<input-test my-value="data.myvalue"></input-test>

angularjs angular-directive

No comments:

Post a Comment