我在绑定模态使用的指令中模态的范围变量时遇到了问题。我已经尝试了网络上的所有解决方案。我也尝试了$parent
解决方案,但似乎没有任何工作。我是Angularjs的新手,所以请帮助我。下面是代码:无法绑定模态的范围变量来自指令angularjs
指令:
.directive('searchPart', function($timeout) {
return {
restrict: 'AEC',
transclude:true,
scope: {
items: '=',
prompt:'@',
title: '@',
subtitle:'@',
model: '=',
onSelect:'&'
},
link:function(scope,elem,attrs){
scope.handleSelection=function(selectedItem){
scope.model=selectedItem;
scope.current=0;
scope.selected=true;
$timeout(function(){
scope.onSelect();
},200);
};
scope.current=0;
scope.selected=true;
scope.isCurrent=function(index){
return scope.current==index;
};
scope.setCurrent=function(index){
scope.current=index;
};
},
templateUrl: 'admin/product/catalogView/partSearch.html'
}
})
模态控制器:
.controller('ChildPartEditCtrl', function ($scope, $modalInstance, Data, $http) {
$scope.name="";
$scope.onItemSelected=function(){
console.log('selected='+$scope.name);
}}
HTML:
<search-part items="items" prompt="Enter full part number" title="name" subtitle="abbreviation" model="name" on-select="onItemSelected()" />
模板
<input type="text" ng-model="model" placeholder="{{prompt}}" ng-keydown="selected=false" />
<br/>
<div class="items" ng-hide="!model.length || selected">
<div class="item"
ng-repeat="item in items | filter:{partnumber:model} track by $index"
ng-click="handleSelection(item.partnumber)" style="cursor:pointer"
ng-class="{active:isCurrent($index)}"
ng-mouseenter="setCurrent($index)">
<p class="title">{{item.partnumber}}</p>
</div>
</div>
哪个绑定不起作用..?你的问题到底是什么..? –
好的,如果你能看到模板,我用item.partnumber值设置模型。在指令中,它将在scope.model中进行设置。在搜索零件指令 –
中甚至包括nooff,零件描述都没有在模态控制器的范围内设置,这同样反映在$ scope.name中。我明白我必须使用对象而不是范围。但我怎么实现从指令太 –