2014-02-16 3 views
0

По какой-то причине мой $scope.$watch не работает? Когда я печатаю, я не вижу обновления в консоли.

(function() { 
    'use strict'; 

    wikiApp.directive('wikiMarkdownEdit', ['pathSvc', function (pathSvc) { 
    var getFullPath = pathSvc.getFullPath; 
    return { 
     restrict: 'E', 
     templateUrl: getFullPath('html/directives/markdownEdit.html'), 
     replace: true, 
     scope: { 
     model: '=', 
     formId: '@', 
     rows: '@', 
     placeholder: '@' 
     }, 
     controller: function($scope, $sce, $attrs, $log) { 
     var converter = new Showdown.converter(); 

     $scope.showPreview = false; 

     /** 
     * Keep the preview up to date 
     */ 
     $scope.$watch('model', updateHTML); 
     var updateHTML = function() { 
      var html = converter.makeHtml($scope.model || ''); 
      console.log(html); 
      // jQuery('#' + $scope.formId + 'Preview').html('1111'); 
     }; 
     updateHTML(); 

     /** 
     * Sync the height of the preview div with the textarea 
     **/ 
     var lastHeight = 0; 
     var getHeight = function() { 
      var newHeight = jQuery('#' + $scope.formId).outerHeight(); 
      if (lastHeight === newHeight || newHeight < 20) { 
      setTimeout(getHeight, 100); 
      return; 
      } 
      lastHeight = newHeight; 
      jQuery('#' + $scope.formId + 'Preview').height(newHeight); 
      setTimeout(getHeight, 100); 
     }; 
     getHeight(); 

     /** 
     * Toggle preview button callback 
     */ 
     $scope.togglePreview = function() { 
      $scope.showPreview = !$scope.showPreview; 
     }; 

     } 
    }; 
    }]); 
})(); 
+0

вы можете показать свой HTML? Возможно, вы не привязываете модель правильно, и когда вы печатаете, свойство не обновляется. –

ответ

0

попытка изменить var updateHTML к function updateHTML, вы не можете использовать функцию, определенную с var перед определением.

другой способ рекомендуется: больше

function updateHTML() { 
    var html = converter.makeHtml($scope.model || ''); 
    console.log(html); 
}; 

$scope.$watch(function() { 
    return $scope.model; 
}, updateHTML); 

Одна вещь, в вашей директиве рамки, вы имели в виду model быть ограничен в ngModel? Если это так, попробуйте этот подход:

... 
scope: { 
    ngModel: '=' 
}, 
... 

Или

... 
scope: { 
    model: '=ngModel' 
}, 
... 
Смежные вопросы