我正在尝试创建一个指令,该指令将使用与创建指令的元素相同的ng-model创建输入字段。
这是我到目前为止所提出的:
HTML
<!doctype html>
<html ng-app="plunker" >
<head>
<meta charset="utf-8">
<title>AngularJS Plunker</title>
<link rel="stylesheet" href="style.css">
<script>document.write("<base href=\"" + document.location + "\" />");</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
This scope value <input ng-model="name">
<my-directive ng-model="name"></my-directive>
</body>
</html>
的JavaScript
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = "Felipe";
});
app.directive('myDirective', function($compile) {
return {
restrict: 'E',
scope: {
ngModel: '='
},
template: '<div class="some"><label for="{{id}}">{{label}}</label>' +
'<input id="{{id}}" ng-model="value"></div>',
replace: true,
require: 'ngModel',
link: function($scope, elem, attr, ctrl) {
$scope.label = attr.ngModel;
$scope.id = attr.ngModel;
console.debug(attr.ngModel);
console.debug($scope.$parent.$eval(attr.ngModel));
var textField = $('input', elem).
attr('ng-model', attr.ngModel).
val($scope.$parent.$eval(attr.ngModel));
$compile(textField)($scope.$parent);
}
};
});
但是,我不相信这是处理这种情况的正确方法,并且有一个错误,我的控件没有使用ng-model目标字段的值进行初始化。
以上是上述代码的一部分:http://plnkr.co/edit/IvrDbJ
处理此问题的正确方法是什么?
编辑:从模板中删除ng-model="value"
后,这似乎工作正常。但是,我会保持这个问题的开放性,因为我想仔细检查这是否正确。
答案 0 :(得分:209)
编辑:这个答案很旧,可能已经过时了。只是抬头,所以它不会让人误入歧途。我不再使用Angular了,所以我不能很好地进行改进。
这实际上是非常好的逻辑,但你可以简化一些事情。
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.model = { name: 'World' };
$scope.name = "Felipe";
});
app.directive('myDirective', function($compile) {
return {
restrict: 'AE', //attribute or element
scope: {
myDirectiveVar: '=',
//bindAttr: '='
},
template: '<div class="some">' +
'<input ng-model="myDirectiveVar"></div>',
replace: true,
//require: 'ngModel',
link: function($scope, elem, attr, ctrl) {
console.debug($scope);
//var textField = $('input', elem).attr('ng-model', 'myDirectiveVar');
// $compile(textField)($scope.$parent);
}
};
});
<body ng-controller="MainCtrl">
This scope value <input ng-model="name">
<my-directive my-directive-var="name"></my-directive>
</body>
.some {
border: 1px solid #cacaca;
padding: 10px;
}
您可以使用此Plunker来查看它。
这就是我所看到的:
编辑正如Mark在评论中提到的那样,没有理由你不能使用ng-model,只是为了遵守惯例。
通常,如果希望指令中的值始终映射到父作用域中的值,则指令应使用隔离作用域(您已正确执行)并使用'='类型作用域。
答案 1 :(得分:66)
我对所有答案进行了组合,现在有两种方法可以使用ng-model属性:
var app = angular.module('model', []);
app.controller('MainCtrl', function($scope) {
$scope.name = "Felipe";
$scope.label = "The Label";
});
app.directive('myDirectiveWithScope', function() {
return {
restrict: 'E',
scope: {
ngModel: '=',
},
// Notice how label isn't copied
template: '<div class="some"><label>{{label}}: <input ng-model="ngModel"></label></div>',
replace: true
};
});
app.directive('myDirectiveWithChildScope', function($compile) {
return {
restrict: 'E',
scope: true,
// Notice how label is visible in the scope
template: '<div class="some"><label>{{label}}: <input></label></div>',
replace: true,
link: function ($scope, element) {
// element will be the div which gets the ng-model on the original directive
var model = element.attr('ng-model');
$('input',element).attr('ng-model', model);
return $compile(element)($scope);
}
};
});
app.directive('myDirectiveWithoutScope', function($compile) {
return {
restrict: 'E',
template: '<div class="some"><label>{{$parent.label}}: <input></label></div>',
replace: true,
link: function ($scope, element) {
// element will be the div which gets the ng-model on the original directive
var model = element.attr('ng-model');
return $compile($('input',element).attr('ng-model', model))($scope);
}
};
});
app.directive('myReplacedDirectiveIsolate', function($compile) {
return {
restrict: 'E',
scope: {},
template: '<input class="some">',
replace: true
};
});
app.directive('myReplacedDirectiveChild', function($compile) {
return {
restrict: 'E',
scope: true,
template: '<input class="some">',
replace: true
};
});
app.directive('myReplacedDirective', function($compile) {
return {
restrict: 'E',
template: '<input class="some">',
replace: true
};
});
.some {
border: 1px solid #cacaca;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
<div ng-app="model" ng-controller="MainCtrl">
This scope value <input ng-model="name">, label: "{{label}}"
<ul>
<li>With new isolate scope (label from parent):
<my-directive-with-scope ng-model="name"></my-directive-with-scope>
</li>
<li>With new child scope:
<my-directive-with-child-scope ng-model="name"></my-directive-with-child-scope>
</li>
<li>Same scope:
<my-directive-without-scope ng-model="name"></my-directive-without-scope>
</li>
<li>Replaced element, isolate scope:
<my-replaced-directive-isolate ng-model="name"></my-replaced-directive-isolate>
</li>
<li>Replaced element, child scope:
<my-replaced-directive-child ng-model="name"></my-replaced-directive-child>
</li>
<li>Replaced element, same scope:
<my-replaced-directive ng-model="name"></my-replaced-directive>
</li>
</ul>
<p>Try typing in the child scope ones, they copy the value into the child scope which breaks the link with the parent scope.
<p>Also notice how removing jQuery makes it so only the new-isolate-scope version works.
<p>Finally, note that the replace+isolate scope only works in AngularJS >=1.2.0
</div>
我不确定我喜欢链接时的编译。但是,如果您只是将元素替换为另一个元素,则不需要这样做。
总而言之,我更喜欢第一个。只需将范围设置为{ngModel:"="}
,然后将ng-model="ngModel"
设置为模板中的所需位置。
更新:我内联了代码段并为Angular v1.2更新了代码段。事实证明,隔离范围仍然是最好的,特别是在不使用jQuery时。所以归结为:
您是否正在替换单个元素:只需替换它,保留范围,但请注意,对于v2.0,不推荐使用replace:
app.directive('myReplacedDirective', function($compile) {
return {
restrict: 'E',
template: '<input class="some">',
replace: true
};
});
否则请使用:
app.directive('myDirectiveWithScope', function() {
return {
restrict: 'E',
scope: {
ngModel: '=',
},
template: '<div class="some"><input ng-model="ngModel"></div>'
};
});
答案 2 :(得分:51)
&#39;不那么复杂:
在您的指令中,使用别名:scope:{alias:'=ngModel'}
.directive('dateselect', function () {
return {
restrict: 'E',
transclude: true,
scope:{
bindModel:'=ngModel'
},
template:'<input ng-model="bindModel"/>'
}
在你的HTML中,正常使用
<dateselect ng-model="birthday"></dateselect>
答案 3 :(得分:30)
当您需要访问模型的$ viewValue或$ modelValue时,您只需要ng-model。见NgModelController。在这种情况下,您可以使用require: '^ngModel'
。
其他方面,请参阅Roys answer。
答案 4 :(得分:18)
This is a little late answer, but I found this awesome post about NgModelController
, which I think is exactly what you were looking for.
TL;DR - you can use require: 'ngModel'
and then add NgModelController
to your linking function:
link: function(scope, iElement, iAttrs, ngModelCtrl) {
//TODO
}
This way, no hacks needed - you are using Angular's built-in ng-model
答案 5 :(得分:2)
我不会通过属性设置ngmodel,您可以在模板中正确指定:
template: '<div class="some"><label>{{label}}</label><input data-ng-model="ngModel"></div>',
答案 6 :(得分:0)
创建隔离范围是不可取的。我会避免使用scope属性并执行类似的操作。 scope:true为您提供新的子范围,但不是隔离。然后使用parse将局部范围变量指向用户提供给ngModel属性的同一对象。
buffer_out = ftp.getwelcome()