先定義一個directive:
app.directive('conversation', [function() {
return {
restrict: 'E',
templateUrl: 'conversation.html?date' + new Date().getTime(),
replace: true,
scope:{
conversations:"=",
changeConversation: "&"
},
link:function(scope, element, attributes){
}
};
}]);
呼叫
<conversation conversations="conversations" change-conversation="changeConversation(conversation)"></conversation>
<ul class="mdui-list">
<li class="mdui-list-item mdui-ripple" ng-repeat="conversation in conversations" ng-click="changeConversation(conversation)">
<p class="mdui-list-item-avatar">
<img ng-src="{{conversation.chatThumbLogo}}"/>
</p>
<p class="mdui-list-item-content">
<p class="mdui-list-item-title">{{conversation.chatName}}</p>
<!--<p class="mdui-list-item-text mdui-list-item-one-line">hello world</p>-->
<span class="im_badge" ng-bind="conversation.unreadCount" ng-if="conversation.unreadCount > 0"></span>
</p>
</li>
<li class="mdui-pider-inset mdui-m-y-0"></li>
</ul>
ng-click觸發函數後不能印出conversation,undefined。
$scope.changeConversation = function (conversation) {
console.log(conversation);
}
這裡是parent的scope,你parent的scope裡並沒有conversation這個變數。
我也覺著是
@熊丸子
说的那样,conversation
這個變數不是父級的conversation 這個directive restrict: 'E', 是element ,你沒有在template引用