我想知道是否可以在运行时断开模型和 View 之间的链接。
在下面的示例中,所有这些都链接在一起(通过 text 模型)。当我单击按钮时,我想让 Angular 不再更新最后的输入(例如启动一些 jquery 效果......)。
<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 示例中,我希望当我按下“+”按钮时,旧值(正在褪色的值)不会再改变。
最佳答案
您可以淡出 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;
});
});
}
}
};
})
关于javascript - angularJS:如何断开模型和 View 之间的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11748917/