指令不接受对象作为参数

时间:2016-06-28 14:01:30

标签: angularjs

我正在使用AngularJS 1.5.7版进行编码。

由于某种原因,该指令未收到cdata变量object

为什么指令不接收变量?

下面我粘贴了所有代码:

home.js

'use strict';

var home = angular.module('app.home', [
  'global.factory',
  'home.factory',
  'ui.scroll',
  'ui.scroll.jqlite',
  'd.card'
]);

home.controller('HomeController', [
  'homeFactory',
  '$timeout',
  function (homeFactory, $timeout) {

    var data = this;

    data.posterSize = (screen.width - 42) / 3;

    homeFactory.getJSON().then(function (response) {
      data.feed = response.home;
    });

    data.datasource = {
      get: function(index, count, success) {
        $timeout(function() {
          var start = Math.max(0, index);
          var end = Math.min(index + count, data.feed.length);

          var results = [];
          for (var i = start; i < end; i++) {
            results.push(data.feed[i]);
          }

          success(results);
        }, 100);
      }
    };
  }
]);

_home.html

<article ng-controller="HomeController as myHome" class="base-gen" ui-scroll-viewport style="height:100%">
  <card class="card" ui-scroll="element in myHome.datasource" cdata="{{element}}" psize="{{myHome.posterSize}}"></card>
</article>

card.js

'use strict';

var dCard = angular.module('d.card', ['global.factory']);

dCard.directive('card', [ 'globalImages', function (globalImages) {
  return {
    restrict: 'E',
    scope: {
      cdata: '@',
      psize: '@'
    },
    controller: function() {
      console.log(this.cdata);
    },
    controllerAs: 'myCard',
    bindToController: true,
    templateUrl: 'app/Modules/card.html'
  };
}]);

我还想指出,我已从angular 1.4.8迁移到1.5.7,从ui-router 0.2.15迁移到0.3.1

1 个答案:

答案 0 :(得分:2)

指令中的绑定表示&#34; =&#34;,这意味着angular将为您执行绑定。无需{{ }}

 <article ng-controller="HomeController as myHome" class="base-gen" ui-scroll-  viewport style="height:100%">
   <card class="card" ui-scroll="element in myHome.datasource" cdata="element" psize="{{myHome.posterSize}}"></card>
 </article>

使用psize我不确定您是否需要{{ }},但对于cdata,则不需要括号