我的HTML
<section class="localsContainer" ng-controller="LocalsCtrl">
<h3>Lokale</h3>
<div class="placesProgress"></div>
<ul>
<li ng-repeat="city in cities">
<p ng-show="city.LocalCount > 0" ng-click="getLocals(city.Id)">{{city.Name}}</p>
<ul ng-show="activeCityLeft === city.Id">
<li ng-repeat="local in locals[city.Id]">
<p>{{local.Name}} ng-click="getDetails()"</p>
</li>
</ul>
</li>
<li ng-if="cities.length == 0">
<strong>Nie znaleziono</strong>
</li>
</ul>
</section>
和js
angular.module('sandboxApp')
.controller('MainCtrl', function ($scope, API) {
API.newRequest('getCitiesList').success(function (data, status, headers, config) {
$scope.cities = data.data;
});
$scope.getDetails = function () {
console.log("test")
};
})
.controller('LocalsCtrl', function ($scope, API) {
$scope.locals = {};
$scope.getLocals = function (cityid) {
$scope.activeCityLeft = cityid;
API.newRequest('getLocalList', { params: { 'city_id': cityid.toString() }, cache: true }).success(function (data, status, headers, config) {
$scope.locals[cityid] = data.data;
});
};
})
如果我正确理解,我应该能够在任何地方调用“ getDetails”方法,因为它是在主控制器上定义的,即使是从获得其won控制器但嵌套在与主控制器相连的元素内的元素中也可以调用它。但是它不起作用,当我单击应调用getDetails的元素时,它会以html文本形式显示“ ng-click =“ getDetails()”。
最佳答案
您的ng-click
定义错误-您将其定义为<p>
元素内的文本:
<p>{{local.Name}} ng-click="getDetails()"</p>
您应该将其定义为属性,因为它是指令:
<p ng-click="getDetails()">{{local.Name}}</p>