【发布时间】:2017-05-26 04:39:41
【问题描述】:
在下面的示例中,我编写了一个函数,该函数在 $scope.word 的默认值下工作正常,但是当我更新视图或输入时,它不会更新控制器中的 $scope,因此它不会反射回视图。不确定这里的数据绑定和摘要循环如何工作。谢谢,
html:
<div class="container" ng-controller="myCtrl">
<textarea rows="10" cols="50" ng-model="word"></textarea>
<br>
{{word}}
<ul>
<li ng-repeat="(key, value) in obj">{{key}} : {{value}}</li>
</ul>
<dir text="largetext"></dir>
</div>
js:
var myApp = angular.module('myApp', []);
myApp.controller('myCtrl', ['$scope', function($scope){
$scope.obj = {}
$scope.word = "d a c a d a b c b b c b d d";
var str = $scope.word.trim().replace(/[\.\,\']/g,"").split(" ");
function wordCount(str) {
var _obj = {}
str.forEach(function(word){
(word in _obj) ? _obj[word]++ : _obj[word] = 1
})
return _obj;
}
$scope.wordCount = wordCount;
console.log($scope.wordCount(str));
$scope.obj = wordCount(str);
}])
【问题讨论】:
-
你的小提琴很好。
标签: javascript angularjs data-binding scope digest