指向控制器的角度全局控制器资源

时间:2013-12-05 23:31:31

标签: angularjs coffeescript angularjs-directive

所以我有点路障。我试图通过一个属性创建一个包含可变行数的小缩略图库,并给定一个属性和一个集合,它可以构建小部件。

因此,我所坚持的部分是从全局控制器(使用后端api的$资源)获取集合传递给指令控制器,因此我可以编写逻辑来拆分和分割集合。行数,并填写每一行,直到它完整"。

所以逻辑是不完整的,因为我不确定如何处理它。我已经尝试了我能找到的所有技巧(使用绑定' ='和表达'&')从全球范围内抓取这个集合,但我似乎没有做什么让我从我的控制器中访问该集合。我所拥有的唯一成功是它作为插值html工作(因为你会在顶部看到rowlink)。

这个有效!我看到我的项目的JSON,但我不想在那里,因为我必须操纵它,我如何将这个集合到我的控制器中进行拼接并重建为单独的项目项?

angular.module("slidePresenter",[])
.controller "presenterCtrl", ($scope, $element, $attrs, $transclude, $compile) ->
  rowLink = $compile('<div presenterRow ngtransclude>{{getItems() | json}}</div>')
  $scope.rowCount = []
  $scope.buildRows = (rows) ->
    $scope.rowCount = (num for num in [rows..1])
  $scope.printRows = ->
    console.log "rowCount is: #{$scope.rowCount.length}"
  $scope.addRows = ->
    angular.forEach $scope.rowCount, (index,value) ->
      console.log value
      clone = rowLink $scope.$parent.$new(), (clone) ->
        $element.append clone.addClass("presenterRow")
      $element.children().each ->
        $(this).height "#{$element.height() / ($scope.rowCount.length + 1)}px"


.directive "presenter", ($compile) ->
  restrict: "A"
  transclude: true
  controller: "presenterCtrl"
  scope: {rows: '=', getItems: '&'}
  compile: (element, attrs, transcludeFn) ->
    return (scope, element, attrs, presenterCtrl) ->
      scope.$watch 'rows', (value) ->
        scope.buildRows(value)
        scope.printRows()
        scope.addRows()



.directive "presenterRow", ->
  restrict: "A"
  replace: true
  require: '^presenter'
  scope: {title: '@', image: '@', thumbnail: '@', snippet: '@', description: '@'}
  compile: (element, attrs, linker) ->
    return (scope, element, attrs, presenterCtrl) ->

我想要的所有内容都封装在这个小部件/组件中......然后是全局空间

angular.module('appolio', ['slidePresenter','ngResource'])

.factory 'Projects' , ($resource) ->
    $resource("/projects/:id", {id: '@id'})


.controller 'mainCtrl' , ($compile, $scope, Projects) ->
  $scope.projects = Projects.query()

  $scope.getItems = ->
    $scope.projects

当然还有标记

<body ng-app="appolio">
  <div class="viewBox" ng-controller="mainCtrl">
    <div class="presenter" presenter getItems="getItems()" rows='4'></div>

我尝试过使用&#39;&amp; expression&#39; &#39; =结合&#39; ,直接将数组作为$ scope.projects传递,并简单地执行类似

的操作
scope: {items: '='}

和标记

<presenter items="projects"></presenter>

但这对我不起作用?当我尝试在控制器中访问它时,我得到的值是&#34; projects&#34;通过访问属性只是字符串&#34; projects&#34;,但$ scope.projects的值为mainCtrl,所以在这一点上 - 我不在乎......我只是需要找出来如何将此集合复制到我的指令控制器并修改它= /

0 个答案:

没有答案