AngularJs Scope,变量值得到改变

时间:2013-04-18 15:44:25

标签: ruby-on-rails-3 twitter-bootstrap angularjs angularjs-directive angularjs-scope

我是AngularJs的新手,我已经学过教程,在过去的5个小时里我遇到了以下问题,

我使用的是twitterbootstrap,而我使用的是modal bootstrap modal

它向我显示模态一次当我点击插槽并保存也是第一次但是第二次,它调用了addBooking方法初始化currentBooking对象,但不显示模态。

这是我对haml的看法,简单显示bootstrap模态及其工作正常。

#scheduler{ :'ng-controller' => 'SchedulerCtrl', :scheduler => true, :data =>{:interval =>  @interval, :restaurant_id => @restaurant_id}  }

  #booking-editor.modal.hide.fade{ :'aria-hidden' => true, :'aria-labelledby' => "bookingEditorLabel", :role => "dialog", :tabindex => "-1", :'booking-editor' => 'currentBooking', :'ng-controller' => 'BookingEditorCtrl' }
    .modal-body
      %input{ :type => "text", :'ng-model' => "currentBooking.name" }
    .modal-footer
      %button.btn{ :'aria-hidden' => true, :'data-dismiss' => "modal" } Close
      %button.btn.btn-primary{ :'ng-click' => 'saveBooking(currentBooking)' } Save Booking

计划程序控制器;

  $scope.addBooking = (slot) ->
    $scope.currentBooking = { name: "John Doe", new: true }

BookingEditorCntl

@BookingEditorCtrl = ['$scope', '$resource', ($scope, $resource) ->
  $scope.saveBooking = (booking) ->
    $scope.currentBooking = null
]

DOM操作的指令部分

  .directive 'bookingEditor', ->
    restrict: 'A'
    link: (scope, element, attrs, ctrl) ->
      scope.$watch attrs.bookingEditor, ->
        if scope[attrs.bookingEditor]
          $(element).modal(keyboard: true)
          $(element).modal('show')
        else
          $(element).modal('hide')

请提出任何建议或想法。感谢。

1 个答案:

答案 0 :(得分:0)

您尚未指定正在使用的模态插件,因此有点难以提供帮助。但是,从您的示例中,您似乎多次调用$(element).modal(keyboard: true)。这可以解释为什么你会收到错误。如果是这种情况,以下内容可能有所帮助:

BookingEditorCtrl

$scope.modalInitialized = false;

bookingEditor指令中

if(scope.modalInitialized === false)
    $(element).modal(keyboard: true)
$(element).modal('show')