angularjs - two way data binding between ng-model and directive -
case :
i trying watch change of select list through directive add html based on selected value reason unable watch change in link function . idea ? thanks
html
<select ng-model="selected" ng-options="item item.type item in itemslist"></select> <d-input type="selected.type"></d-input>
js
app.directive('dinput', function($compile) { return { restrict: 'e', scope : { type : '=', } , template : '<div></div>', link : function (scope, element, attrs) { scope.$watch(scope.type, function() { var tmplt = '' ; if (scope.type == 'input') tmplt = '<input type ="text" name="inputname" value="0">'; if (scope.type== 'select') tmplt = '<select ><option> option1</option><option>option2</option></select>'; if (scope.type == 'radio') tmplt = '<input type ="radio" name="inputname" value="0">'; element.html(tmplt); $compile(element.contents())(scope); }); }, } });
you need change first parameter of $watch
function:
scope.$watch(function () { return scope.type; }, function() { .... });
Comments
Post a Comment