且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在AngularJS指令双向数据绑定

更新时间:2021-10-31 09:02:55

我不知道你为什么手动触发 $适用方法,因为你其实并不需要它。

I don't know why you are triggering the $apply method manually because you actually don't need it.

我编辑您从角页面中使用的例子,包括输入。
它为我工作: http://jsfiddle.net/6HcGS/2/

I edited the example you used from the Angular page and included the input. It works for me: http://jsfiddle.net/6HcGS/2/

HTML

<div ng-app="zippyModule">
  <div ng-controller="Ctrl3">
    Title: <input ng-model="title">
    <hr>
    <div class="zippy" zippy-title="title"></div>
  </div>
</div>​

JS

function Ctrl3($scope) {
  $scope.title = 'Lorem Ipsum';
}

angular.module('zippyModule', [])
  .directive('zippy', function(){
    return {
      restrict: 'C',
      replace: true,
      transclude: true,
      scope: { title:'=zippyTitle' },
      template: '<input type="text" value="{{title}}"style="width: 90%"/>',
      link: function(scope, element, attrs) {
        // Your controller
      }
    }
  });

更新
maxisam是正确的,你必须使用 NG-模型而不是针对像这样的值绑定变量:

UPDATE maxisam is right, you have to use ng-model instead of binding the variable against the value like so:

<input type="text" ng-model="title" style="width: 90%"/>

下面是工作的版本: http://jsfiddle.net/6HcGS/3/