2014-10-21 2 views
1

Я делаю директиву У меня странное поведение, когда я делаю консоль по области . Я получаю значение, переданное из области контроллера, напечатанное, но когда я пытаюсь получить к нему доступ объем не определен как scope.modelдиректива Isolated Scope undefined, angularjs 1.3

это код:

function() { 
     var color = d3.interpolateRgb('#f77', '#77f'); 
     return { 
      template: '<div></div>', 
      restrict: 'E', 
      scope:{ 
       model:'=' 
      }, 
      link: function postLink(scope, element, attrs) { 

       console.log(scope); //print model 
       console.log('scope.model ',scope.model); // undefined 

       var width = attrs.width || 940, 
        height = attrs.height || 500, 
        margin = 20; 

       var modeler = d3.select(element[0]) 
        .append('svg') 
        .attr('width', width) 
        .attr('height', height); 

      } 
     }; 
    } 

HTML

<d3-directive model="model" width="920" height="510" ></d3-directive> 

Пожалуйста, обратите внимание на выход консоли: console output

ответ

1

Я в конечном итоге, добавив $ пекутся значения объема и с использованием нового значения, присвоенного.

return { 
     template: '<div></div>', 
     restrict: 'E', 
     scope:{ 
      model:'=' 
     }, 
     link: function postLink(scope, element, attrs) { 

      console.log(scope); 
      console.log('scope.model ',scope.model); 

      scope.$watch('model',function(newValue,oldValue){ 
       if(!!newValue){ 
        console.log(newValue); 
       } 
      }); 

      var width = attrs.width || 940, 
       height = attrs.height || 500, 
       margin = 20; 

      var modeler = d3.select(element[0]) 
       .append('svg') 
       .attr('width', width) 
       .attr('height', height); 

     } 
    }; 
+1

Имейте точно такую ​​же проблему. Кажется, использование $ watch - единственный вариант – lostaman