指令没有't更改文本区域模型后发生火灾
Directive doesn't fire after changing textarea model
我有一个带有换行符和URL的文本:
CCD_ 1。
我想在按下copy
按钮后更新ng-repeat
值。
我有这个HTML:
<div ng-controller="myCntrl">
<textarea ng-model="copy_note_value"></textarea>
<button data-ng-click="copy()">copy</button>
<div>
<p ng-repeat="row in note_value.split(''n') track by $index"
wm-urlify="row"
style="display: inline-block;"
>
</p>
</div>
</div>
控制器:
app.controller('myCntrl', function ($scope) {
$scope.note_value = "first row'nFind me at http://www.example.com and also'n at http://stackoverflow.com";
$scope.copy_note_value = angular.copy($scope.note_value);
$scope.copy = function(){
$scope.note_value = angular.copy($scope.copy_note_value);
}
});
我有一个指令,应该接受文本并返回URL化文本:
app.directive('wmUrlify', ['$parse', function ($parse) {
return {
restrict: 'A',
scope: true,
link: function (scope, element, attrs) {
function urlify(text) {
var urlRegex = /(https?:'/'/[^'s]+)/g;
return text.replace(urlRegex, function (url) {
return '<a href="' + url + '" target="_blank">' + url + '</a>';
})
}
var text = $parse(attrs.wmUrlify)(scope);
var html = urlify(text);
element[0].inneHtml(html)
}
};
}]);
下面是一个流程:用户更改textarea
中的文本并按下copy
按钮。我希望展示ng-repeat
的变化。
只有当我添加新行而不添加行内容时,它才有效。
这里怎么了?这是我的Fiddle
只需从ng-repeat
中删除track by $index
即可。这是因为您告诉Angular,只有当first row'nFind me at http://www.example.com and also'n at http://stackoverflow.com
0发生变化时,note_value.split(''n')
的值才会发生变化,即通过新行分割后的阵列大小。
但是track by
的默认实现是每个项目的标识。因此,当您更改默认实现以通过$index
跟踪它时,并且当您不添加新行而只是更新任何现有行的内容时,Angular无法检测到有更改。
更新
当拆分后有相同的值时,删除track by $index
函数将引发异常。所以你可以使用一个简单的函数,比如:(在你的控制器中定义它)
$scope.indexFunction = function($index, val) {
// Creating an unique identity based on the index and the value
return $index + val;
};
然后在你的ng-repeat
中使用它,比如:
<p ng-repeat="row in note_value.split(''n') track by indexFunction($index, row)"></p>
https://docs.angularjs.org/api/ng/directive/ngRepeat
相关文章:
- 在VanillaJS中模拟模型双向数据绑定
- onkeyup无法动态创建多个文本区域
- FabricJs-限制主对象内添加对象的移动区域
- 为集合分配大量的模型弹药
- 如何使用backbone.js从集合中获取模型名称
- 骨干模型默认值-todos.js示例中不必要的代码
- EmberJS中支持单字母单词模型
- 2个backbone.js集合,具有相同的模型,但排序顺序不同
- 将不在模型中的数据返回到mvc控制器
- 具有所有样式的文本正在复制到可编辑文本区域
- 如何为高图中的区域线创建z索引
- 如何在视图模型contet更新更新上调用Jquery函数
- 如何为动态创建的文本区域中输入的值更新ng模型
- 如何在手动插入一些文本后更新文本区域的ng模型
- Ng模型在AngularJS中重置文本区域后未更新
- 当文本区域绑定到模型时,angularjs中的默认文本区域值
- 关闭模态不't清除文本区域数据,但它确实清除了模型
- 用ng重复或ng模型填充一个文本区域,这样它就不会用迭代的数据一个接一个地创建新的文本区域
- 指令没有't更改文本区域模型后发生火灾
- 修改 Django 表单.模板中的文本区域或始终在模型中