我在重用相同的指令时遇到问题。我无法在指令内外设置值 谁能帮我?非常感谢 这是代码
angular.module('customControl', []).
directive('myDir', function($compile) {
return {
restrict: 'E',
require: '?ngModel', // get a hold of NgModelController
scope:{
ngModelList:'=ngModel'},
link: function(scope, element, attrs, ngModel) {
if(!ngModel) return;
var newInput = "<input ng-model = 'childModel1' ng-click = 'getItem()'>";
if(attrs.multimode=='true'){
newInput = "<input ng-model = 'childModel1' ng-click = 'getItem2()'>";
}
element.html(newInput);
$compile(element.contents())(scope);
scope.getItem2 = function() {
var d = new Date();
scope.childModel2 = 'item2_' + d.getYear();
ngModel.$setViewValue(scope.childModel1);
console.log(scope.childModel2);
scope.ngModelList = scope.childModel2;
console.log('list: '+scope.childModel1);
}
scope.getItem =function(){
var d = new Date();
scope.childModel1 = 'item1_' + d.getMonth();
ngModel.$setViewValue(scope.childModel1);
console.log(scope.childModel1);
scope.ngModelList = scope.childModel1;
console.log('list: '+scope.childModel1);
}
}
}
});