gpt4 book ai didi

javascript - 在 AngularJS 中,如何将变量从父作用域传递到子作用域

转载 作者:行者123 更新时间:2023-11-30 07:14:27 24 4
gpt4 key购买 nike

我有一个部分页面的代码类似于以下内容:

<div class="selector-result row">
<div ng-if="resultCtrl.result">
<div class="col-xs-12">
<h4><strong>We Recommend:</strong></h4>
<h2><strong>{{resultCtrl.result.Name}}</strong></h2>
</div>
<div class="row">
<div class="col-md-4">

<div ng-controller="selectorResultCarouselController">
<div>
<div style="height: 305px">
<carousel interval="myInterval" no-wrap="false">
<slide ng-repeat="slide in slides" active="slide.active" actual="slide">
<img ng-src="{{slide.image}}" style="margin:auto;">
</slide>
</carousel>
</div>
</div>
</div>
...

我有一个模块,它有一个带有 controllerAs resultCtrl 的指令 (selectorResult)。那里也有一个 Controller ,selectorResultController,它加载一个变量'results'。

我想做的是以某种方式将 {{resultCtrl.result.AllImages}} 放入 selectorResultCarouselController 中,以便我可以将它们添加到我的轮播中。我迷路了。我真的很努力去理解 Angular,我想我理解这些部分是如何工作的,我只是不理解这个系统,如果这有意义的话。

我只是想在这里寻找一点插入力。我读了又读又读,但我没有看到任何能说明这个问题的东西。

最佳答案

为避免 $scope 混淆,请考虑使用 AngularJS's controllerAs syntax .基本上,不是将值附加到 $scope,而是将它们附加到 Controller 对象本身。所以:

angular.module('myApp', [])
.controller('ctrlOne', [function() {
var self = this;
self.name = 'ctrlOne';
}])
.controller('ctrlTwo', [function() {
var self = this;
self.name = 'ctrlTwo';
}]);

<div ng-app="myApp">
<div ng-controller="ctrlOne as one">
<div ng-controller="ctrlTwo as two">
<p>{{one.name}}</p> <!-- 'ctrlOne' -->
<p>{{two.name}}</p> <!-- 'ctrlTwo' -->
</div>
</div>
</div>

关于javascript - 在 AngularJS 中,如何将变量从父作用域传递到子作用域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33000659/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com