2016-07-31 8 views
2

I Winkel Richtlinie haben, die mich wie dieses

Es gibt

app.directive('paymentsTable', ['$watch', function($watch) { 
    return { 
     replace: true, 
     restrict: 'EACM', 
     templateUrl: '../views/paymentTable.html', 
     link: function(elem, attr, scope) { 

     console.log(elem.$parent.payments); // array 

     scope.$watch(function(elem) { return elem.$parent.payments }, function(value, oldValue) { 

     }); 
    } 
    }; 
}]); 

sieht

angular.js:13920Error: [$injector:unpr]

Als ich das erste Zeile wie diese

app.directive('paymentsTable', [ function() { 

umschreiben Es gibt mir ein anderer Fehler

angular.js:13920TypeError: o.$watch is not a function

Ich benutze auch Uglify. Meine Frage ist also: Was geht hier vor?

Antwort

1

Die $watch Funktion ist Teil von scope, die Ihnen in der link Methode übergeben wird, daher gibt es keine Notwendigkeit, es zu injizieren. Der Grund für den zweiten Fehler ist die Reihenfolge der Argumente link. Versuchen Sie es so:

app.directive('paymentsTable', function() { // no need for injection 
    return { 
     replace: true, 
     restrict: 'EACM', 
     templateUrl: '../views/paymentTable.html', 
     link: function(scope, element, attrs) { // The correct arguments order 

     console.log(elem.$parent.payments); 

     scope.$watch(function(elem) { return elem.$parent.payments }, function(value, oldValue) { 

     }); 
    } 
    }; 
}); 
+0

Vielen Dank. –

+0

viel Glück mit Ihrer App – AranS