gpt4 book ai didi

javascript - 通过父 Controller AngularJS 设置指令元素属性

转载 作者:行者123 更新时间:2023-12-02 18:22:29 26 4
gpt4 key购买 nike

我想通过我的应用程序级别 Controller 配置我的指令模块。 <强> Plunker

index.html

<div ng-controller="App">
<foodz index="index"></foodz>
</div>

app.js

angular.module('app', ['foodz']).
controller('App', ['$scope',function($scope){
$scope.index = 1;
}]);

foodz.js

angular.module('foodz', []).
controller('foodzController', ['$scope',function($scope){

//Data is coming in through external API
$scope.$on('foodzFetched', function(e,d) {
$scope.foodz = d;
});

//Lets say data to looks like:
//[{"name":"banana"},{"name":"smoothy"}]
}]).
directive('foodz', function() {
return {
restrict: 'E',
scope:{
index: '@'
},
replace: true,
controller: 'foodzController',
templateUrl: 'foodzTemplate.html',
link: function(scope, controller) {}
};
});

foodzTemplate.html

<div ng-controller="foodzController">
<span>
{{foodz[index].name}}
</span>
</div>

因此,在这个示例中,我尝试将 index 通过我的 app 级别 Controller 传递到我的指令元素的属性中,该元素也有自己的 Controller 。

我在这里做错了什么?

最佳答案

尝试使范围:{index:'='}

看看Directive Definition Object

我认为您正在 Controller 中接收 index='index' (作为字符串)。使用 = 它将从父 Controller 获取值。

关于javascript - 通过父 Controller AngularJS 设置指令元素属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18702171/

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