Angularjs 安格拉斯赢得';t更新视图
我刚接触angularJS,遇到了第一个大问题。 首先,我想创建一个列表并更新它,但它不起作用Angularjs 安格拉斯赢得';t更新视图,angularjs,Angularjs,我刚接触angularJS,遇到了第一个大问题。 首先,我想创建一个列表并更新它,但它不起作用 // index.html <form ng-controller="ListCtrl"> <div class="form-group"> <input name="listname" ng-model="listname"> <input type="button" ng-click="set()" value="a
// index.html
<form ng-controller="ListCtrl">
<div class="form-group">
<input name="listname" ng-model="listname">
<input type="button" ng-click="set()" value="add new list">
</div>
</form>
<div ng-controller="ListCtrl">
<ul>
<li ng-repeat="list in lists">
{{list.id}} - {{list.listname}}
</li>
</ul>
{{output}}
</div>
编辑:
当我使用$apply尝试此操作时,控制台显示一个错误:
function resetForm() {
$scope.$apply(function () {
$scope.listname = "";
$scope.listForm.$setPristine();
$scope.output = 'Hello World';
});
}
错误是:
Uncaught TypeError: undefined is not a function
错误直接指向“$scope.$apply(函数…”的开头
编辑2:
当我在index.html中添加一个新按钮并用ng调用一个函数时,单击,在这个函数中我只说
$scope.output = 'Hello World!';
然后我的视图更新。
当我使用回调来改变作用域时,它不会更新。我不明白。我以为一切都是在angularJS中连接的,特别是当我在同一个控制器中时
编辑3:
这是你的电话号码
未捕获类型错误:未定义不是函数
这是因为您调用了$scope.apply
而不是$scope.$apply
尝试使用回调函数调用它:
$scope.$apply(function () {
$scope.listname = "";
$scope.listForm.$setPristine();
$scope.output = 'Hello World';
});
索引中有两个ng controller=“ListCtrl”。html@sylwester我不确定,但我认为您可以将同一个控制器链接到两个不同的视图。我使用两次链接的控制器进行了测试,效果良好。请显示更多代码。
listForm
在哪里定义?在哪里设置()
method get called?@runTarm我更新了我的原始帖子方法在index.html中通过submit调用。您还需要什么代码?这是简单的angularJS结构。这更好。您尝试过我上面发布的代码段吗?不,将回调传递到$scope.$apply
,它可以被称为$scope.$apply()
毫无例外,不管它是否应该这样调用。@Tipo我的错误,你应该调用$apply
而不是apply
。没问题,我已经修复了它。但这并不能解决我的问题。:-(我看不出我的失败。我怎么能看出来我是否超出了我的范围?也许我不在范围内?但通常我不相信。好吧,原始帖子中添加了plunker
$scope.$apply(function () {
$scope.listname = "";
$scope.listForm.$setPristine();
$scope.output = 'Hello World';
});