mardi 10 février 2015

AngularJs: Set Model null on change in Directive

I Want to change the model in directive , on change of checkbox in directive .



<input type="checkbox" ng-model="isCheck" />
<select ng-model="empNmae" ng-setempty="isCheck"
ng-options="em.Value as em.Name for em in emp">
<option value="">Select
</select>
{{empNmae}}


JS



// Code goes here
var app=angular.module('myApp',[]);
app.controller("mycntrl", function ($scope) {
$scope.emp=[{
"Name":"Amit",
"Value":101
},{
"Name":"Amit2",
"Value":103
},{
"Name":"Amit3",
"Value":102
}];
$scope.empNmae=102;

});

app.directive('ngSetempty', function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, contro) {
scope.$watch(attrs.ngSetempty,function(scope, element, attrsArg, contro){
if(this.last){
alert(attrsArg.empNmae);
// Set Model of select Null
}else{
//Reset the model of select to intial
}
});
}
};
});


I want to set model of select to null , if checkbox is checked, and he uncheck the checkbox, then set the model of select to intial value.


MY Plunker





Aucun commentaire:

Enregistrer un commentaire