2016-04-12 84 views
0

我想將一個動態查詢字符串傳遞給一個指令,它將過濾返回的結果。所以,我的代碼如下所示:將字符串和變量傳遞給指令屬性。

在我的控制器所以說,我有這樣的:

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

然後在我的HTML我有一個指令:

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

然後在我的指導我是引入像「=過濾器」的位置。我希望指令中的輸出是「location = USA」,但是指令中的輸出只是「USA」。

關於我在做什麼錯的任何建議?

+1

模板試試這個過濾器= 「lctrlAs.location」 –

+0

我意識到自己的錯誤,我發佈之後。我需要filter =「'location ='+ ctrlAs.location」 –

回答

0

試試這個。

或更改指令這樣

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>

相關問題