输入范围的属性指令会破坏与父控制器的ng-model绑定

时间:2013-10-31 12:33:00

标签: javascript angularjs

我有一个带控制器的div元素。 div元素包含一个输入元素,其中ng-model绑定到一个对象。 input元素还有一个属性指令。

如果我将一个范围添加到属性指令中,则控制器中的ng-model绑定会中断。有没有办法让这个工作,或者我应该找一个解决方法?

您可以在http://jsbin.com/IvIFobU/4/处看到代码。

HTML

<p>The input field below should say "foobar":</p>
<div ng-controller="fooController">
  <input ng-model="object.string" foo-attribute="callback">
</div>

的JavaScript

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

app.controller("fooController", function($scope) {
      $scope.object = {
        string: 'foobar'
      };

      $scope.callback = function() {
        console.log('callback');
      };
});

app.directive('fooAttribute', function() {
  return {
    restrict: 'A',

    // This scope breaks the ng-model on the input... 
    scope: {
      callback: '&fooAttribute'
    },

    link: function(scope, element, attr) {
      element.css('background', 'lightblue');

      scope.callback();
    }
  };
});

0 个答案:

没有答案
相关问题