我希望在ng-repeat内部的ng-click帮助中包含html页面。
但是,它加载了所有ng-repeat元素的所有内容。
我的要求是我想仅绑定(ng-include)被点击的元素
请找附件供您参考。
HTML
<body ng-app="myApp" ng-controller="myCtrl">
<div ng-repeat="message in messages">
<a ng-click="clickMe()">Clike Me</a>
<span>{{ message.text }}---{{ message.type }}</span>
<div ng-include="templateUrl"></div>
</div>
</body>
JS
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope) {
var messages = [
{ text: "Standard Message", type: "success000" },
{ text: "Success Message!", type: "success111" },
{ text: "Alert Message!", type: "alert222" },
{ text: "secondary message...", type: "secondary333" }
]
$scope.name = 'shiva';
$scope.count = 0;
$scope.messages = messages;
$scope.clickMe = function () {
// alert('clicked');
$scope.count++;
$scope.templateUrl = "Page.html";
};
});
page.Html
<b> Included html Code.{{message.type}}---count={{count}}
答案 0 :(得分:2)
添加到message
新媒体资源clicked
。仅在使用ng-include
message.clicked
为true
时设置ng-if
。当您click
时,请更改message.clicked
true
的状态
像这样:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl',function($scope){
var messages = [
{ text: "Standard Message", type: "success0" ,clicked:false},
{text:"Success Message!", type:"success00",clicked:false},
{text:"Alert Message!", type : "alert2",clicked:false},
{text:"secondary message...", type : "secondary3",clicked:false}
]
$scope.name = 'shiva';
$scope.count = 0;
$scope.messages = messages;
$scope.clickMe = function (message) {
// alert('clicked');
$scope.count++;
message.clicked=true;
$scope.templateUrl = "Page.html";
};
});
</script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
<div ng-repeat="message in messages">
<a ng-click="clickMe(message)">Clike Me</a>
<span>{{message.text}}---{{message.type}}</span>
<div ng-if="message.clicked" ng-include="templateUrl"></div>
</div>
</body>
</html>
答案 1 :(得分:0)
首先,您需要添加id
您的json数据,如下所示:
var messages = [
{ id: "101", text: "Standard Message", type: "success0", clickCount: 0 },
{ id: "102", text: "Success Message!", type: "success00", clickCount: 0 },
{ id: "103", text: "Alert Message!", type: "alert2", clickCount: 0 },
{ id: "104", text: "secondary message...", type: "secondary3", clickCount: 0 }
]
接下来,您需要运行filter
并在ng-click
$scope.clickMe = function (obj) {
$scope.messages.forEach(function (value, key) {
if (value.id === obj.id) {
value.isClicked = true;
value.clickCount++;
}
});
$scope.count++;
$scope.templateUrl = "Page.html";
};
之后,对 Page.html
进行更改<p class="info">{{ name }}--{{ message.type }}--{{ message.text }}</p>
<p class="element">
Current Count = ({{ message.clickCount }}), Total Count = ({{ count }})
</p>
最后,您需要进行以下两次HTML更改
1)在ng-click
<a ng-click="clickMe(message)">Clike Me</a>
2)在ng-include
<div ng-include="templateUrl" ng-if="message.isClicked"></div>