2016-04-12 15 views
0

私は動的なクエリ文字列を返す結果をフィルタするディレクティブに渡そうとしています。だから私のコードは次のようになります。文字列と変数をディレクティブ属性に渡します。

だから私はこれを持って私のコントローラで言う:

var ctrlAs = this; 
ctrlAs.location = "USA"; 

その後、私のhtmlで私はディレクティブを持っている:

<directive filter="location=ctrlAs.location"> 

その後、私の指示に私は"= filter"のような場所を持ってきます。私はディレクティブの出力が "location = USA"であると期待しますが、ディレクティブの出力は単に "USA"です。

私が間違っていることに関する提案はありますか?

+1

のような変更指示のテンプレート= "lctrlAs.location" –

+0

私は私が投稿した直後に私のミスを実現し、このフィルタを試してみてください。私はフィルタ= "'場所=' + ctrlAs.location ' –

答えて

0

このようにしてください。

ORこの

template : '<div><span><label>location=</label>{{filter}}</span></div>' 

var myApp = angular.module('MyApp',[]) 
 
myApp.directive('directive', function() { 
 
    return { 
 
     restrict: 'E', 
 
     replace: true, 
 
     scope: { 
 
      filter: '=' 
 
     }, 
 
     template : '<div><span>{{filter}}</span></div>', 
 

 
     controller: function($scope, $element, $attrs, $location) {   
 
     } 
 
    } 
 
}); 
 
     
 

 
myApp.controller('MyController', function($scope, $window) { 
 
    $scope.location = 'USA'; 
 
    
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="MyApp"> 
 
    <div ng-controller="MyController"> 
 
     <directive filter="'location='+location" /> 
 
    </div> 
 
</div>

関連する問題