Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在angular中通过函数更新视图_Javascript_Angularjs_Data Binding_Scope_Digest - Fatal编程技术网

Javascript 如何在angular中通过函数更新视图

Javascript 如何在angular中通过函数更新视图,javascript,angularjs,data-binding,scope,digest,Javascript,Angularjs,Data Binding,Scope,Digest,在下面的示例中,我编写了一个默认值为$scope.word的函数,但当我更新视图或输入时,它不会更新控制器中的$scope,从而不会反射回视图。不确定这里的数据绑定和摘要循环是如何工作的。谢谢 html: 以下是工作代码: <div class="container" ng-controller="myCtrl"> <textarea rows="10" cols="50" ng-model="word" ng-change="wordCount()"></

在下面的示例中,我编写了一个默认值为$scope.word的函数,但当我更新视图或输入时,它不会更新控制器中的$scope,从而不会反射回视图。不确定这里的数据绑定和摘要循环是如何工作的。谢谢

html:


以下是工作代码:

<div class="container" ng-controller="myCtrl">
    <textarea rows="10" cols="50" ng-model="word" ng-change="wordCount()"></textarea>
    <br>
    {{word}}
    <ul>
        <li ng-repeat="(key, value) in obj">{{key}} : {{value}}</li>
    </ul>
    <dir text="largetext"></dir>
</div>


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";
    function wordCount() { 
    var str = $scope.word.trim().replace(/[\.\,\']/g,"").split(" ");
        $scope.obj = {}
        str.forEach(function(word){
            (word in $scope.obj) ? $scope.obj[word]++ : $scope.obj[word] = 1
        })
        return $scope.obj;
    }
    $scope.wordCount = wordCount;
    console.log($scope.wordCount());
    $scope.obj = wordCount(); 
}])


{{word}}
  • {{key}}:{{{value}
var myApp=angular.module('myApp',[]); 控制器('myCtrl',['$scope',函数($scope){ $scope.obj={} $scope.word=“d a c a d a b c b d”; 函数wordCount(){ var str=$scope.word.trim().replace(/[\.\,\']/g,“”)。split(“”); $scope.obj={} str.forEach(函数(字){ (单词在$scope.obj中)?$scope.obj[word]+:$scope.obj[word]=1 }) 返回$scope.obj; } $scope.wordCount=wordCount; log($scope.wordCount()); $scope.obj=wordCount(); }])
以下是工作代码:

<div class="container" ng-controller="myCtrl">
    <textarea rows="10" cols="50" ng-model="word" ng-change="wordCount()"></textarea>
    <br>
    {{word}}
    <ul>
        <li ng-repeat="(key, value) in obj">{{key}} : {{value}}</li>
    </ul>
    <dir text="largetext"></dir>
</div>


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";
    function wordCount() { 
    var str = $scope.word.trim().replace(/[\.\,\']/g,"").split(" ");
        $scope.obj = {}
        str.forEach(function(word){
            (word in $scope.obj) ? $scope.obj[word]++ : $scope.obj[word] = 1
        })
        return $scope.obj;
    }
    $scope.wordCount = wordCount;
    console.log($scope.wordCount());
    $scope.obj = wordCount(); 
}])


{{word}}
  • {{key}}:{{{value}
var myApp=angular.module('myApp',[]); 控制器('myCtrl',['$scope',函数($scope){ $scope.obj={} $scope.word=“d a c a d a b c b d”; 函数wordCount(){ var str=$scope.word.trim().replace(/[\.\,\']/g,“”)。split(“”); $scope.obj={} str.forEach(函数(字){ (单词在$scope.obj中)?$scope.obj[word]+:$scope.obj[word]=1 }) 返回$scope.obj; } $scope.wordCount=wordCount; log($scope.wordCount()); $scope.obj=wordCount(); }])
演示代码

<div class="container" ng-controller="myCtrl">
    <textarea rows="10" cols="50" ng-change="wordCount(word)" ng-model="word"></textarea>
    <br>
    {{word}}
    <ul>
        <li ng-repeat="(key, value) in obj">{{key}} : {{value}}</li>
    </ul>
    <dir text="largetext"></dir>
</div>


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";

    $scope.wordCount = function(str) { 
  str = str.trim().replace(/[\.\,\']/g,"").split(" ");
        $scope.obj = {}
        str.forEach(function(word){
            (word in $scope.obj) ? $scope.obj[word]++ : $scope.obj[word] = 1
        })    
    }       
  $scope.wordCount($scope.word);
}])


{{word}}
  • {{key}}:{{{value}
var myApp=angular.module('myApp',[]); 控制器('myCtrl',['$scope',函数($scope){ $scope.obj={} $scope.word=“d a c a d a b c b d”; $scope.wordCount=函数(str){ str=str.trim().replace(/[\.\,\']/g,“”)。split(“”); $scope.obj={} str.forEach(函数(字){ (单词在$scope.obj中)?$scope.obj[word]+:$scope.obj[word]=1 }) } $scope.wordCount($scope.word); }])
演示代码

<div class="container" ng-controller="myCtrl">
    <textarea rows="10" cols="50" ng-change="wordCount(word)" ng-model="word"></textarea>
    <br>
    {{word}}
    <ul>
        <li ng-repeat="(key, value) in obj">{{key}} : {{value}}</li>
    </ul>
    <dir text="largetext"></dir>
</div>


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";

    $scope.wordCount = function(str) { 
  str = str.trim().replace(/[\.\,\']/g,"").split(" ");
        $scope.obj = {}
        str.forEach(function(word){
            (word in $scope.obj) ? $scope.obj[word]++ : $scope.obj[word] = 1
        })    
    }       
  $scope.wordCount($scope.word);
}])


{{word}}
  • {{key}}:{{{value}
var myApp=angular.module('myApp',[]); 控制器('myCtrl',['$scope',函数($scope){ $scope.obj={} $scope.word=“d a c a d a b c b d”; $scope.wordCount=函数(str){ str=str.trim().replace(/[\.\,\']/g,“”)。split(“”); $scope.obj={} str.forEach(函数(字){ (单词在$scope.obj中)?$scope.obj[word]+:$scope.obj[word]=1 }) } $scope.wordCount($scope.word); }])
你的小提琴很好。你的小提琴很好。谢谢你的回答。为什么在wordCount函数中再次声明$scope.obj?不,我没有声明。。我已分配空对象。。。所以字符数应该是当前的。。。否则它也会转到++最后计数的字符..谢谢你的回答。为什么在wordCount函数中再次声明$scope.obj?不,我没有声明。。我已分配空对象。。。所以字符数应该是当前的。。。否则,它也将转到++最后计数的字符。。