控制器中的文本永远不会记录/永远不会运行Angular Rails

时间:2015-07-03 02:36:43

标签: javascript ruby-on-rails angularjs coffeescript

我正在Rails中构建一个应用程序,我正在使用Angular作为前端的一部分,根据json文件中提供的数据生成视图。我一直在关注this教程,但我似乎无法让我的控制器运行,我不知道为什么。任何人都可以帮助我吗?

示例链接:http://localhost:3000/events/events1

events.coffee.erb

APP_LOG_PATH=logs/abc.log

events.html.erb

app = angular.module('EventsApp', ['ui.router', 'templates'])

app.config [
  '$stateProvider'
  '$urlRouterProvider'
  ($stateProvider, $urlRouterProvider) ->
    $stateProvider.state 'events',
      url: '/events/{id}'
      templateUrl: 'templates/_event.html'
      controller: 'EventsCtrl'
]

app.controller 'EventsCtrl', [
    '$scope'
    '$stateParams'
    ($scope, $stateParams) ->
        console.log $stateParams //this never shows up
        console.log 'Sup' //this never shows up
        $scope.greeting = 'Yo' //neither does this
]

_event.html

<div ng-app="EventsApp">
    Yo! //this shows up, but not {{greeting}}
    {{greeting}}
    <ui-view></ui-view>
</div>

Rails路由文件:

<script type="text/ng-template" id="/event.html">
    <div>
        {{greeting}}
        Yo!
    </div>
</script>

Rails events_controller.rb

get "events/*path" => "events#events"

文件布局:

class EventsController < ApplicationController
    layout "application"

    def events
    end
end

1 个答案:

答案 0 :(得分:0)

您的角度代码结构不佳。我想你是在用你的Ruby语法混合你的Javascript语法...

尝试更接近这一点:

angular.module('EventsApp', ['ui.router', 'templates'])
  .config(['$stateProvider', '$urlRouterProvider', 
    function($stateProvider, $urlRouterProvider) {
      $stateProvider.state('events', {
        url = '/events/{id}',
        templateUrl: 'templates/_event.html',
        controller: 'EventsCtrl'
      }
    }
  ])
  .controller('EventsCtrl', [
    '$scope', '$stateParams'
    function($scope, $stateParams) {
      console.log($stateParams); 
      console.log('Sup')
      $scope.greeting = 'Yo';
    }
  ]);