2014-10-30 76 views
0

我注射值到定制指令,但该值未在指令模板显示:AngularJS DI字符串转换成指令

app.value('messageString', { 
    'to' : 'TO', 
    'startDate' : 'START DATE', 
    'endDate' : 'END DATE', 
}); 
app.directive('dateRangePicker', function(messageString) { 
    return { 
     replace: true, 
     restrict: 'EA', 
     template: '<div class="input-group">' + 
         '<input type="text" class="form-control" placeholder="{{messageString.startDate}}" id="start-date" ng-model="startdate">' + // doesn't work in placeholder 
        '</div>', 
     link: function(scope, element, attributes) { 
      console.log(messageString.startDate); // works here 
     } 
    }; 
}); 

那么,什么地方出了错?

+2

你必须将其连接到示波器的性能序结合工作,在你的链接函数做: - 'scope.messageString = messageString' – PSL 2014-10-30 01:33:11

回答

2

您需要将messageString设置成scope如果你要在模板中使用它,就像这样:

app.value('messageString', { 
    'to' : 'TO', 
    'startDate' : 'START DATE', 
    'endDate' : 'END DATE', 
}); 
app.directive('dateRangePicker', function(messageString) { 
    return { 
     replace: true, 
     restrict: 'EA', 
     template: '<div class="input-group">' + 
         '<input type="text" class="form-control" placeholder="{{messageString.startDate}}" id="start-date" ng-model="startdate">' + // doesn't work in placeholder 
        '</div>', 
     link: function(scope, element, attributes) { 
      scope.messageString=messageString; 
      console.log(messageString.startDate); // works here 
     } 
    }; 
}); 
+0

噢,你说得对,谢谢! – dulan 2014-10-30 01:36:29