您只需爲它坐下(plnkr)即可創建服務:
.service('birthMonth', function() {
this.getBirthMonth = function (DOB) {
var month = [];
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
var d = new Date(DOB),
n = month[d.getMonth()];
return n; // returns monthName i.e February
}
});
指令:
.directive('myCustomer', ['birthMonth', function(birthMonth) {
return {
restrict: 'E',
scope: {
customerInfo: '=info'
},
templateUrl: 'my-customer-plus-vojta.html',
link: function (scope) {
scope.getBirthMonth = birthMonth.getBirthMonth;
}
};
}])
HTML:
<p ng-repeat="x in customerInfo">
Name: {{x.name}} Address: {{x.address}}
<span> {{getBirthMonth(x.DOB)}}</span>
</p>
我不會鼓勵th尤其是在具有隔離範圍的指令中使用'$ parent'。 –