我在第一个模块中显示搜索到的内容详细信息。我需要显示特定记录的描述。我在Image.Here我的代码中使用了ng-click。请给出一些解决方案。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<div class="container">
<div ng-app="mainApp" ng-controller="mainController">
<input type="text" ng-model="test.count">
<table>
<tr ng-repeat="x in names | filter:test">
<td><a href="#" ng-click="displayDescription(x)"><img src="images/{{ x.image}}.png" /></a><span>{{ x.name }}</span></td>
<td>{{ x.brand }}</td>
<td>{{ x.count }}</td>
</tr>
</table>
</div>
<div id="sub" ng-app="subApp" ng-controller="mainController">
<div>{{dummyContent}}
<p ng-repeat="x in names">{{ x.desc }}</p>
</div>
</div>
</div>
<script>
var mainApp = angular.module('mainApp',[]);
mainApp.controller('mainController',function($scope) {
$scope.names=[
{image:'German',name:'Sebastin Vettel',brand:'Red Bull',count:'397',desc:'Red Bull is an energy drink'},
{image:'India',name:'Shanmuga Sundaram',brand:'Kingfisher',count:'242',desc:'Kingfisher is favourite brand in India'},
{image:'Brazilian',name:'Mark Webber',brand:'Kingcobra',count:'199',desc:'Kingcobra is very worst brand i ever drink'},
{image:'Spanish',name:'Lewis Hamilton',brand:'Budweiser',count:'189', desc:'Budweiser is Best brand i ever drink'},
{image:'German',name:'Kimi Raikonnan',brand:'Fosters',count:'183', desc:'Fosters is next to brand Budweiser'}
];
});
var subApp = angular.module('subApp',['mainApp']);
subApp.controller('mainController',function($scope) {
$scope.dummyContent = "dummt text";
$scope.displayDescription = function(x) {
$scope.Description = x.desc;
};
});
angular.bootstrap(document.getElementById("sub"),['subApp']);
</script>
</body>
</html>
我在第二个模块中分配了一个新的变量dummyContent。我可以访问它。但我需要从第一个模块访问说明{{x.desc}}。
答案 0 :(得分:1)
最好通过服务共享通用代码。只需使用Angular提供者模型之一创建服务,并将其注入任何需要的地方。