【发布时间】:2015-03-19 09:35:57
【问题描述】:
我在 Ionic 框架中执行此操作。它以home.html 模板和eventmenu.home 状态开始。 $scope.aaa 变量是 10,它正确显示了该值。但是,在该模板的子控制器 TestCtrl 中,我无法更改 $scope.aaa 值并对其进行更新。 我的方法有什么问题,为什么aaa 没有增加? TestCtrl 是否与 HomeCtrl 处于不同的范围内,尽管它应该是 HomeCtrl 的子级,不是吗?
代码:
http://codepen.io/hawkphil/pen/xbmZGm
HTML:
<script id="templates/home.html" type="text/ng-template">
<ion-view view-title="Welcome" >
<ion-content class="padding" >
<p>Swipe to the right to reveal the left menu.</p>
<p>(On desktop click and drag from left to right)</p>
<p>{{ aaa }}</p>
<div ng-controller="TestCtrl">
<a ng-click="clickMe()" href="">Click Me</a>
</div>
</ion-content>
</ion-view>
</script>
JS:
.controller('HomeCtrl', function($scope) {
$scope.aaa = 10;
})
.controller('TestCtrl', function($scope) {
$scope.clickMe = function() {
$scope.aaa++;
}
})
p.s 我正在使用别人的代码,所以请忽略其他内容。
【问题讨论】:
-
为什么不直接使用服务呢?除非我在这里遗漏了什么?此外,您提供的此代码不反映您链接到的代码笔
-
我可以使用服务,但这不是问题。我的问题是关于 $scope 的继承原则。看我的附言
标签: javascript angularjs angularjs-scope angular-ui-router ionic-framework