【问题标题】:My ng-change is not firing on my Angular/Rails app我的 ng-change 没有在我的 Angular/Rails 应用程序上触发
【发布时间】:2014-07-27 19:59:57
【问题描述】:

我正在构建一个记事卡应用程序,但由于某种原因,我的 ng-change 根本没有触发。我无法弄清楚问题是什么。我在 JS 中尝试了断点来验证它实际上没有触发。我很肯定这一定是我错过的一件小事。我只需要第二双眼睛。

这里是JS

var app = angular.module('catalyst', ['faye']);

app.factory('Faye', [
  '$faye', function($faye) {
    return $faye("http://localhost:9292/faye");
  }
]);

app.directive('stickyNote', function(Faye) {
  var linker = function(scope, element, attrs) {
      element.draggable({
        stop: function(event, ui) {
          Faye.publish('/ui/board', {
            id: scope.note.id,
            x: ui.position.left,
            y: ui.position.top
          });
        }
      });

      Faye.subscribe('/ui/board', function(data) {
        // Update if the same note
        if(data.id == scope.note.id) {
          element.animate({
            left: data.x,
            top: data.y
          });
        }
      });

      // Some DOM initiation to make it nice
      element.css('left', '10px');
      element.css('top', '50px');
      element.hide().fadeIn();
    };

  var controller = function($scope) {
      // Incoming
      Faye.subscribe('/ui/board', function(data) {
        // Update if the same note
        if(data.id == $scope.note.id) {
          $scope.note.title = data.title;
          $scope.note.body = data.body;
        }       
      });

      // Outgoing
      $scope.updateNote = function(note) {
        Faye.publish('/ui/board', note);
      };

      $scope.deleteNote = function(id) {
        $scope.ondelete({
          id: id
        });
      };
    };

  return {
    restrict: 'A',
    link: linker,
    controller: controller,
    scope: {
      note: '=',
      ondelete: '&'
    }
  };
});

app.controller('MainCtrl', function($scope, Faye) {
  $scope.notes = [];

  // Incoming
  Faye.subscribe('/ui/board', function(data) {
    $scope.notes.push(data);
  });

  Faye.subscribe('/ui/board', function(data) {
    $scope.handleDeletedNoted(data.id);
  });

  // Outgoing
  $scope.createNote = function() {
    var note = {
      id: new Date().getTime(),
      title: 'New Note',
      body: 'Pending'
    };

    $scope.notes.push(note);
    Faye.publish('/ui/board', note);
  };

  $scope.deleteNote = function(id) {
    $scope.handleDeletedNoted(id);

    Faye.publish('/ui/board', {id: id});
  };

  $scope.handleDeletedNoted = function(id) {
    var oldNotes = $scope.notes,
    newNotes = [];

    angular.forEach(oldNotes, function(note) {
      if(note.id != id) newNotes.push(note);
    });

    $scope.notes = newNotes;
  }
});

这里是ui/board.html.haml

%body{"ng-controller" => "MainCtrl"}
  %nav.top-bar{"data-topbar" => ""}
    %ul.title-area
      %li.name
        %h1
          %a{:href => "#"} AngularJS CollabBoard
      %li.toggle-topbar.menu-icon
        %a{:href => "#"}
          %span Menu
    %section.top-bar-section  
      %ul.right
        %li
          %a#createButton{"ng-click" => "createNote()"} Create Note
  .alert-box.success.radius.sticky-note{"ng-repeat" => "note in notes track by $index", :note => "note", :ondelete => "deleteNote(id)", "sticky-note" => ""}
    %button.close{"ng-click" => "deleteNote(note.id)", :type => "button"} ×
    %input.title{"ng-change" => "updateNote(note)", "ng-model" => "note.title", :type => "text"}
      %textarea.body{"ng-change" => "updateNote(note)", "ng-model" => "note.body"} {{note.body}}

【问题讨论】:

  • 不是因为你没有把控制器代码放在你的实际控制器MainCtrl里面吗?还是打算将其包含在指令中?如果 deleteNote 有效,但 updateNote 无效,那可能就是它的来源。

标签: javascript ruby-on-rails angularjs


【解决方案1】:

这里有很多东西要看,我相信你有范围问题。一方面,您没有在指令上使用 transclude,因此您的子元素不会包含在编译的指令中。我还注意到您的主控制器上有 deleteNote 并将 upwords 委派给主控制器,但随后将 updateNote 放在指令上。我想你的删除是有效的。您正在使用 ng-repeat 它确实为每个“注释”创建了一个子范围。

【讨论】:

  • 啊,好的,谢谢!我想你可能是对的。我之前也没有使用过 ng-transclude 。这是我的第二个 Angular 应用程序。我的删除有点工作。它正在删除元素,然后在没有默认文本的情况下重建元素。这真的很奇怪,但我认为这只是告诉我我需要回到我的教程。谢谢你的帮助。我觉得范围界定是个问题。
  • @TaylorMitchell,当您希望指令包含任意内容时使用 transclude:true - 任意内容具有 ng-transclude 指令。有例子,很高兴能帮上忙
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-31
  • 1970-01-01
相关资源
最近更新 更多