0
我輸入和選擇標籤可用的所有源代碼的指令複合在plunker綁定範圍不會改變正確
我是新手在angularJS,這是閱讀一些教程後,我的第一個指令。 當我更改選擇選項綁定範圍item1不能正確更改!!!! 我希望ITEM1必須與所選的項目,selectedchanged功能
由於更新到任何幫助或改善
codecombo指令模板:
<span>
<input ng-model="code" ng-change="codechanged()">
<select ng-model="selecteditem" ng-change="selectchanged()" ng-options="item as item[propertytitle] for item in list" class="codecombo-select"></select>
</span>
codecombo腳本:在
app.directive('codecombo', function($compile) {
return {
restrict:'A',
scope:{
propertyid:'@',
propertycode:'@',
propertytitle:'@',
list:'=' ,
selecteditem:'=',
selectedchanged:'&'
},
templateUrl:'template.html',
link:
{
pre: function (scope, element, attrs, controller) {
/*make some property optional*/
if(!attrs.propertytitle) attrs.propertytitle="title";
if(!attrs.propertyid) attrs.propertyid="id";
if(!attrs.propertycode) attrs.propertycode="code";
},
post: function (scope, element, attrs, controller) {
var select=element.find('select');
var input=element.find('input');
/*some code delete for save space ...*/
scope.code;
/*select option changed*/
scope.selectchanged=function(){
console.log('select-changed');
console.log(scope.selecteditem);
if(scope.selecteditem!==null){
fillSelectedProperties(scope.selecteditem);
}
else
emptySelectedProperties();
scope.selectedchanged();
}
/*input text changed*/
scope.codechanged=function(){
console.log('code-changed');
scope.selecteditem=findByCode(scope.list,scope.code);
console.log(scope.selecteditem);
scope.selectchanged();
}
}
}
}
});
codecombo使用index.html:
<div codecombo list="testlist" selecteditem="item1" selectedchanged="selectedchanged()"></div>
這是控制器:
app.controller('MainCtrl', function($scope) {
$scope.testlist=[
{id:1,code:100,title:'Item no.100'},
{id:2,code:200,title:'Item no.200'},
{id:3,code:300,title:'Item no.300'},
{id:4,code:400,title:'Item no.400'}
];
$scope.selectedchanged=function(){
/*
i want do some staff with item1 here and expect to be updated for each selectedchanged
but seem it has previous value
*/
console.log($scope.item1);
$scope.title=$scope.item1.title;
}
});
感謝您的回答,是「更改偵聽器(ng-change)在更新外部作用域的字段之前觸發」正常的角度行爲或一些錯誤的指令,我希望對此答案進行投票,但這是我的第一個問題,顯然我需要15點聲望才能投票。 – medusa
你的代碼沒有任何內在錯誤,所以我猜這是正常的行爲。順便說一句,你可以標記這個答案是正確的(這可以改變,如果出現更好的答案),無論你的聲譽如何。 –