 
       return el.val(_formatted(modelCtrl.$modelValue))
 
+    _init = () ->
+      el.daterangepicker opts, (start, end, label) ->
+        $timeout(()->
+          modelCtrl.$setViewValue({startDate: start, endDate: end})
+        )
+        modelCtrl.$render()
 
-    _init = ->
-      el.daterangepicker(opts, (start, end, label) ->
-        $timeout(->
-          $scope.$apply(->
-            modelCtrl.$setViewValue({
-              startDate: start
-              endDate: end
-            })
-            modelCtrl.$render()
-          ))
-      )
-      _picker = el.data('daterangepicker')
-      el
+    _picker = el.data('daterangepicker')
 
     _init()
 
     # If input is cleared manually, set dates to null.
-    el.change(() ->
+    el.change () ->
       if $.trim(el.val()) == ''
-        $timeout(->
-          $scope.$apply(->
-            modelCtrl.$setViewValue(
-              startDate: null
-              endDate: null
-            )
-          ))
-    )
+        $timeout ()->
+          modelCtrl.$setViewValue(
+            startDate: null
+            endDate: null
+          )
 
     if attrs.min
       $scope.$watch('dateMin', (date) ->

  scope:
    dateMin: '=min'
    dateMax: '=max'
    opts: '=options'
  link: ($scope, element, attrs, modelCtrl) ->
    el = $(element)
    customOpts = $parse(attrs.dateRangePicker)($scope, {})
    opts = angular.extend({}, dateRangePickerConfig, customOpts)
    _picker = null

    _formatted = (viewVal) ->
      f = (date) ->

    modelCtrl.$formatters.push((val) ->
      if val and val.startDate and val.endDate
        # Update datepicker dates according to val before rendering.
        _setStartDate(val.startDate)
        _setEndDate(val.endDate)
        return val
      return ''
    )
