wie ng-change-Handler dynamisch Eingang
app.directive('textForm', function ($compile) {
return {
restrict: 'E',
replace: true,
templateUrl: "textForm.html",
scope: {
label: "@",
model: "=",
type: "@",
name: "@",
},
link: function (scope, element) {
scope.log = function() {
console.log(1);
};
var input = element.find('.form-control');
input.attr({'ng-change': 'log()'});
$compile(element.contents())(scope);
}
}
});
<!-- this is textForm.html -->
<div class="form-group">
<label class="col-xs-3 control-label">{{label}}</label>
<div class="col-xs-7">
<input type="{{type||'text'}}" name="{{name}}" class="form-control" ng-model="model">
</div>
</div>
<!-- this is how I use the directive -->
<text-form label="name" name="name" model="person.name"></text-form>
der obige Code konnte nicht laufen, kann aber sagen, das Problem hinzuzufügen.
Ich möchte ng-change
zu input
dynamisch aus irgendeinem Grund hinzufügen und nach einigem Suchen fand ich $compile
könnte dies tun. Aber es scheint nicht zu funktionieren und ich weiß nicht warum.
Meine Winkel Version ist 1.5.5
Wohin beziehen Sie Ihre 'Anweisung' in Ihrem HTML? –
Was ist der Anwendungsfall dafür? – georgeawg
@troy was du in 'ng-change' dynamisch hinzufügen wirst & basierend auf was ?? –