Angularjs 如何访问Angular Directive test内输入字段中的值?
我的角度控制器有一个Jasmine测试,具有以下功能:Angularjs 如何访问Angular Directive test内输入字段中的值?,angularjs,Angularjs,我的角度控制器有一个Jasmine测试,具有以下功能: function compileDirective() { var body = '<div class="col-sm-4">' + '<input id="email" type="email" ng-model="form.email" value="test@test.com" field-match="emailsMatch()">' + '</div>'; v
function compileDirective() {
var body = '<div class="col-sm-4">' +
'<input id="email" type="email" ng-model="form.email" value="test@test.com" field-match="emailsMatch()">' + '</div>';
var tpl = '<form name="myForm">' + body + '</form>';
inject(function($compile) {
var form = $compile(tpl)(scope);
elem = form.find('div');
});
function emailsMatch() {
//do some logic in here, depending on the value of the email field
}
scope.$digest();
}
函数compileDirective(){
变量体=“”+
'' + '';
var tpl=''+主体+'';
注入(函数($compile){
变量形式=$compile(tpl)(范围);
elem=form.find('div');
});
函数emailsMatch(){
//根据email字段的值,在此处执行一些逻辑
}
范围。$digest();
}
我希望能够在compileDirective函数(emailsMatch())中创建另一个函数,该函数查看输入字段中的值并使用它执行一些逻辑操作。但我不知道如何访问输入字段。我有一个elem的句柄,它是从开始的整个html。如何查看输入字段的值?要访问
emailsMatch
函数,您应该将其作为范围的一部分。另一方面,如果您的标记中写入了ng model=“form.email”
,我假设您有表单
(其中包含email
属性)您的范围中的属性。所以你需要这样的东西:
scope.emailsMatch = function() {
console.log( scope.form.email ); //should print value of the <input>
};
scope.emailsMatch=function(){
console.log(scope.form.email);//应打印
};