gpt4 book ai didi

javascript - 使模板在单击 Angular 指令中的链接功能时可见

转载 作者:行者123 更新时间:2023-12-02 13:47:50 25 4
gpt4 key购买 nike

问题的措辞可能不正确。但这就是我正在尝试做的事情。

我有一个使用国家数组定义的navbar,其中包含国家/地区的名称和坐标。

<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">Welcome to the world of directives!</a>
</div>
<ul class="nav navbar-nav">
<li ng-repeat="countryTab in countries" ng-click="itemClicked(countryTab.label)" style="cursor:pointer">
<a>{{countryTab.label}}</a>
<country-tab-bar country="selectedCountry"></country-tab-bar>
</li>
</ul>
</div>
</nav>
<script>
var app = angular.module('app',[]);
app.controller('appCtrl',function($scope){
$scope.countries = [{
id: 1,
label: 'Italy',
coords: '41.29246,12.5736108'
}, {
id: 2,
label: 'Japan',
coords: '37.4900318,136.4664008'
}, {
id: 3,
label: 'USA',
coords: '37.6,-95.665'
}, {
id: 4,
label: 'India',
coords: '20.5937,78.9629'
}];
});
</script>

现在 country-tab-bar 是具有模板的指令,该模板使用数组中定义的坐标显示名称和 map 。

我试过了

    app.directive('countryTabBar',function(){
return {
restrict: 'E',
scope:{
country: '='
},
template: '<div>'+
' <div>Italy</div>'+
' <br/>'+
' <img ng-src="https://maps.googleapis.com/maps/api/staticmap?center={{country.coords}}&zoom=4&size=800x200"> '+
'</div>',
link : function(scope,elem,attrs){
scope.itemClicked = function(value){
scope.selectedCountry = value;
}
}
}
});

但是单击国家/地区名称时没有任何反应。

用户界面现在搞砸了。

enter image description here

需要做什么来解决这个问题?请建议。 map 仅应在单击名称后出现,而不是在此之前。

最佳答案

对您的代码进行一些细微的更改,并且它正在运行。 请参阅下面的评论

<强> WORKING FIDDLE

    //HTML
<div ng-app="app">
<div ng-controller='appCtrl'>
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">Welcome to the world of directives!</a>
</div>
<ul class="nav navbar-nav">
<!-- pass country to itemClicked function defined into controller -->
<li ng-repeat="country in countries" ng-click="itemClicked(country)" style="cursor:pointer">
<a>{{country.label}}</a>
</li>
<!-- directive moved outside the ng-repeat -->
<country-tab-bar country="selectedCountry"></country-tab-bar>
</ul>
</div>
</nav>
<div>
</div>

//app
var app = angular.module('app',[]);

//controller
app.controller('appCtrl',function($scope){
$scope.countries = [{
id: 1,
label: 'Italy',
coords: '41.29246,12.5736108'
}, {
id: 2,
label: 'Japan',
coords: '37.4900318,136.4664008'
}, {
id: 3,
label: 'USA',
coords: '37.6,-95.665'
}, {
id: 4,
label: 'India',
coords: '20.5937,78.9629'
}];

// function to select the country (receive de full object as parameter)
$scope.itemClicked = function(selected){
// set the object needed by the directive
$scope.selectedCountry = selected
}

});

//directive
app.directive('countryTabBar',function(){
return {
restrict: 'E',
scope:{
country: '='
},
template: '<div>'+
' <br/>'+
' <img ng-src="https://maps.googleapis.com/maps/api/staticmap?center={{country.coords}}&zoom=4&size=800x200"> '+
'</div>',
link : function(scope,elem,attrs){

}
}
});

关于javascript - 使模板在单击 Angular 指令中的链接功能时可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41238822/

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