只需要解释为什么它不起作用?我试图从教程的角度来学习教程。第一个和第二个模型运行正常。但不是第三种模式。有人可以向我解释为什么它不起作用吗?提前致谢。
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
</script>
..........
<body ng-app="mainApp" ng-controller="machineController">
<input type="text" ng-model="anyText.first" placeholder="any text here" />
<input type="text" ng-model="anyText.second" placeholder="any text here" />
<span>{{ anyText.third() }}</span>
<script>
var app = angular.module("mainApp", []);
app.controller('machineController', function($scope) {
$scope.anyText = {
first: "This is first default text",
second: "This is second default text",
third: function() {
$object = $scope.anyText;
var newtext = anyText.first + " ::: " + anyText.second;
return newtext;
}
};
});
</script>
</body>
最佳答案
您必须替换此行var newtext = anyText.first + " ::: " + anyText.second;
与
var newtext = $object.first + " ::: " + $object.second;
由于未定义
anyText
变量<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
</script>
<body ng-app="mainApp" ng-controller="machineController">
<input type="text" ng-model="anyText.first" placeholder="any text here"/>
<input type="text" ng-model="anyText.second" placeholder="any text here"/>
<span>{{ anyText.third() }}</span>
<script>
var app = angular.module("mainApp", []);
app.controller('machineController',function($scope){
$scope.anyText = {
first: "This is first default text",
second: "This is second default text",
third: function(){
$object = $scope.anyText;
var newtext = $object.first + " ::: " + $object.second;
return newtext;
}
};
});
</script>
</body>