我已经创建了类似下拉的自定义指令,但在我的示例中,项目在模式弹出中打开,在选择项之后将在div中选择。
现在,我添加了两个指令(,即同一个指令的两个实例),并使用这两个指令作为父级子方式。我从rest中获取项,然后将项分配给第一个指令(,即父指令),并根据第一个指令的选择过滤另一个rest,然后分配给第二个指令(,即子指令),它工作得很好。但是,每当更改第一个指令的选择时,我都希望重置第二个指令的值(所选项)。
我将下面的代码添加到我的指令中,但它并没有帮助我。
controller: ['$scope', '$rootScope',
function($scope, $rootScope) {
$scope.$watch('value', function(val) {
$rootScope.$broadcast('valueChanged', $scope.id);
});
$scope.psChanged = function() {
$scope.$on('valueChanged', function(event, value) {
if (value === $scope.id) {
//Do nothing.
} else {
console.log("Text change");
}
});
}
}
]My full working plunker http://plnkr.co/edit/f6LYS2aGrTXGkZ3vrIDD?p=preview
有人能帮我把它做完吗!!
发布于 2015-01-27 08:13:32
我在孤立作用域中添加了一个名为isChild的属性,当它真正侦听事件valueChanged时。因此,我甚至在没有函数psChanged的控制器中绑定侦听器,而且它可以工作。
angular.module('plexusSelect', []).directive('plexusSelect', ['$ionicModal', '$timeout', '$ionicScrollDelegate', '$rootScope',
function($ionicModal, $timeout, $ionicScrollDelegate, $rootScope) {
// Runs during compile
return {
scope: {
'items': '=',
'id': '@',
'text': '@',
'textIcon': '@',
'headerText': '@',
'textField': '=',
'valueField': '@',
'callback': '&',
'isChild' : '@'
},
require: 'ngModel',
restrict: 'E',
templateUrl: 'plexusSelect.html',
controller: ['$scope', '$rootScope',
function($scope, $rootScope) {
$scope.$watch('value', function(val) {
$rootScope.$broadcast('valueChanged', $scope.id);
});
if ($scope.isChild === 'true') {
//$scope.psChanged = function() {
$scope.$on('valueChanged', function(event, value) {
if ($scope.id === value) {
//Do nothing.
} else {
$scope.clearSearch();
$scope.value = '';
$scope.text = $scope.defaultText;
console.log("Text change");
}
});
}
}
],html
<ion-view view-title="Search" ng-controller='SearchCtrl'>
<ion-content>
<h1>Search</h1>
<plexus-select id="psDeparture" is-child='false' items="deptStations" header-text="Select Departure Station" text="Select departure..." text-icon="icon-takeoff" text-field="['City_Name_EN','City_Code']" value-field="City_Code" ng-model="deptStation.value"></plexus-select>
<plexus-select id="psArrival" is-child='true' items="arrStations" header-text="Select Arrival Station" text="Select arrival..." text-icon="icon-landing" text-field="['Destination.City_Name_EN','Destination.City_Code']" value-field="Destination.City_Code" ng-model="arrStation.value"></plexus-select>
</ion-content>
</ion-view>扑通
https://stackoverflow.com/questions/28165229
复制相似问题