Angular js无法将值传递给指令

时间:2015-12-29 14:44:09

标签: javascript angularjs angularjs-directive

我试图在角度js中创建一个自定义日历,我试图将一些值传递给它的孤立范围,但我没有在指令的范围内得到它无法弄明白为什么

app.directive("calendar", calendar);

function calendar() {
  return {
    restrict: "E",
    templateUrl: "calendar.html",
    scope: {
      selected: "=",
      show: "=",
      avilDates: "="
    },
    link: function(scope) {
      console.log('scope.avilDates' + scope.avilDates);
    }
  }
}
app.controller('addDetailExpController', function($scope) {
  $scope.avilDates1 = [];
  $scope.$watch('avilDates', function() {
    console.log('avilDates', $scope.avilDates)
  });
  $scope.genAvailDate = function(exp) {
    console.log('genAvailDate Working');
    console.log(exp);
    var result = [];
    exp.options.forEach(function(option) {
      if (typeof option.slots != 'undefined') {
        option.slots.forEach(function(slot) {
          if (typeof slot.dates != 'undefined') {
            slot.dates.forEach(function(date) {
              result.push(moment.unix(date.date).format("MM/DD/YYYY"));
            });
          }
        });
      }
    });
    console.log(result);
    return result.reduce(function(a, b) {
      if (a.indexOf(b) < 0) a.push(b);
      return a;
    }, []).map(function(a) {
      return new Date(a);
    });
  }

});
<div ng-controller="addDetailExpController" ng-init="avilDates=genAvailDate(selected)">
  <calendar selected="addDate.date" show="showdate" avilDates="avilDates" ng-show="showdate"></calendar>
</div>

我从genAvailDate得到结果但是我无法在指令

中得到它

1 个答案:

答案 0 :(得分:0)

<div ng-controller="addDetailExpController" ng-init="avilDates=genAvailDate(selected)">
  <calendar selected="addDate.date" show="showdate" avil-dates="avilDates" ng-show="showdate"></calendar>
</div>

尝试使用avil-dates代替avilDates