grid-url
ディレクティブからURLパラメータを読み取れるテーブルを実装しようとしています。その後、これはコントローラー内の$http
に供給されます。 しかし、期待どおりに動作していないようです。値は常に定義されていません。ここでコントローラ内のディレクティブ値を読み取ることができません
がマークアップだ:
<table class="table table-striped" grid-url="http://localhost/records/all">
...
</table>
ここでは、関連する初期の抜粋です:
app.directive('gridUrl', function(){
return {
replace: true,
link: function(scope, element, attrs){
// Add the gridUrl property to the scope
scope.gridUrl = attrs.gridUrl;
}
}
});
app.controller('Ctrl', function($scope){
// Expect to get http://localhost/records/all, but get undefined instead
console.log($scope.gridUrl);
});
それはスコープの分離に問題だったかのように見えません。コントローラー自体にconsole.log($scope)
を呼び出すと、最も奇妙なことは、$scope.gridUrl
にはhttp://localhost/records/all
があることがわかります。
したがって、コントローラーでプロパティーgridUrl
が未定義になる原因は何ですか?
おそらくコントローラ関数をディレクティブと呼びます。あなたは '$ timeout(function {){console.log($ scope.gridUrl);}、1000);で確認することができます; –