Javascript 无法从指令angularjs绑定模态的范围变量
在模态使用的指令中绑定模态的范围变量时遇到问题。我在网上尝试了所有的解决方案。我也尝试过Javascript 无法从指令angularjs绑定模态的范围变量,javascript,angularjs,bootstrap-modal,Javascript,Angularjs,Bootstrap Modal,在模态使用的指令中绑定模态的范围变量时遇到问题。我在网上尝试了所有的解决方案。我也尝试过$parent解决方案,但似乎没有任何效果。我是Angularjs的新手,请帮帮我。代码如下: 指令: .directive('searchPart', function($timeout) { return { restrict: 'AEC', transclude:true, scope: {
$parent
解决方案,但似乎没有任何效果。我是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:
模板
<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.selected = {name: ''}
然后将所选的作为模型传递:
<search-part items="items" prompt="Enter full part number" title="name" subtitle="abbreviation" model="selected" on-select="onItemSelected()" />
并在指令中设置scope.model.name=selectedItem
,这样应该可以工作
这是因为对象是按值传递的,对象引用(内存位置)是值。因此scope.model
将指向所选的控制器作用域对象scope.model.name=selectedItem
将更新其name
属性
原语通过其实际值传递,在原始代码中,作用域。模型根本不指向控制器作用域。哪个绑定不起作用。。?您的问题是什么?好的,如果您能看到模板,我正在使用item.partnumber值设置模型。在指令中,它是在scope.model中设置的。bu搜索部件方向中的$scope.name中没有反映相同的情况,即使nooff,部件描述也没有在模态控制器的范围内设置。我知道我必须使用对象而不是范围。但我如何才能从一吨多的食物中实现这一点呢!工作得很有魅力
<search-part items="items" prompt="Enter full part number" title="name" subtitle="abbreviation" model="selected" on-select="onItemSelected()" />