0

I want to set modelvalue Null in directive, on changing checkbox checked or unchecked.

HTML

<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;
$scope.isCheck=true;
});

app.directive('ngSetempty', function () {
    return {
        restrict: 'A',
        require: 'ngModel',

        link: function (scope,element, attrs, ctrl) {
         scope.$watch(attrs.ngSetempty, function () {
                var getChekc=this.last;
                if(getChekc){
                 // set Model is NULL
                }
            });

        }
    };
});

if checkbox is checked , Then I want to set the model is Null for selectbox. Plunker

Amit Kumar
  • 5,888
  • 11
  • 47
  • 85

2 Answers2

1

Have you tried a simple :

delete $scope.empNmae

?

  • But how would I know the "empNmae" . It will be different for different control,, as i'll use this directive on multiple control. – Amit Kumar Feb 11 '15 at 10:30
0

Add ng-checked=myFunction() attribute to your checkbox and clear the model in myFunction().

HTML:

<select ng-model="empNmae" ng-setempty="isCheck" 
    ng-options="em.Value as em.Name for em in emp" ng-checked="clearModel()">

JS:

$scope.clearModel = function(){
    $scope.empNmae = null;
}

Source.

Angular ngChecked.

Community
  • 1
  • 1
Rahul Desai
  • 15,242
  • 19
  • 83
  • 138