2012-07-31 60 views
8

我想知道是否可以在运行时中断模型和视图之间的链接。
在以下示例中,所有链接在一起(通过文本模型)。当我点击按钮时,我想让角度不再更新最后一个输入(例如开始一些jQuery效果...)。angularJS:如何打破模型和视图之间的链接

<html ng-app> 
    <head> 
    <script src="angular-1.0.1.js"></script> 
    </head> 
    <body> 
    <input ng-model="text"/><br/> 
    <input ng-model="text"/><br/> 
    <input ng-model="text"/><input type="button" value="<- break!" ng-click="???"/><br/> 
    </body> 
</html> 

我的真实情况是在这里:http://jsfiddle.net/5JZPH/10/
在的jsfiddle例子中,我想到的是,旧的价值观念(这是正在消退)不发生任何变化,当我按下“+”按钮。

+1

什么是小提琴的预期/期望行为? – btford 2012-07-31 23:35:08

+0

所以如果你删除ng-model属性它不会失去关联? – 2012-08-01 18:47:10

+0

不,它不工作:( – 2012-08-01 22:15:47

回答

4

您可以淡出jQuery的克隆HTML元素:http://jsfiddle.net/5JZPH/29/

HTML:

<div ng-app="test"> 
    <input type="button" value=" + " ng-click="index = index + 1"/> 
    <input ng-model="index" smooth="index" style="display:block"/> 
    [{{index}}] 
</div> 

的JavaScript:

angular.module('test', []) 
.directive('smooth', function() { 
    return { 
     transclude: 'element', 
     priority: 750, 
     terminal: true, 
     compile: function(element, attr, linker) { 
      return function(scope, iterStartElement, attr) { 

       var prevClone = null; 

       scope.$watch(attr.smooth, function() { 

        var newScope = scope; 

        linker(newScope, function(clone) { 

         if (prevClone) { 

          newPrevClone = prevClone.clone(); 
          prevClone.after(newPrevClone); 
          prevClone.remove(); 
          newPrevClone.fadeOut(2000, function() { $(this).remove() }); 
         } 

         iterStartElement.after(clone); 

         prevClone = clone; 
        }); 
       }); 
      } 
     } 
    }; 
}) 
相关问题