将字符串和变量传递给指令属性。

时间:2016-04-12 15:10:10

标签: angularjs angularjs-directive

我正在尝试将动态查询字符串传递给指令,该指令将过滤返回的结果。所以我的代码看起来像这样:

所以在我的控制器中说我有这个:

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

然后在我的HTML中我有一个指令:

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

在我的指令中,我带来了像“= filter”这样的位置。我希望指令中的输出是“location = USA”,而是指令中的输出只是“USA”。

对我做错了什么建议?

1 个答案:

答案 0 :(得分:1)

尝试这样。

或者像这样更改指令模板

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';
   
});

&#13;
&#13;
<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>
&#13;
 public void setTimer(Timer timer, int seconds, String userName, String content, VBox tabContent,ArrayList<Integer> countTweetsArray, Label statusLabel) {
 TabContent tabContentObj = new TabContent();
 timer.schedule(new TimerTask() {
     @Override
     public void run() {
         setTweet(userName, content);
         //tabContentObj.createStatusScreen(tabContent, countTweetsArray, remainingTweets);
         System.out.println(content+"  after  "+seconds);
         System.out.println("countTweetsArray: "+countTweetsArray.get(0));
         statusLabel.setText(countTweetsArray.get(0).toString());
         countTweetsArray.set(0, (countTweetsArray.get(0)+1));
         tabContentObj.timersMap.put(userName, timer);
     }
 }, (seconds*1000));
 }
&#13;
&#13;
&#13;