gpt4 book ai didi

javascript - 具有隔离范围 : How to set ngModel to the correct scope? 的指令中的表单输入

转载 作者:行者123 更新时间:2023-11-30 17:57:21 25 4
gpt4 key购买 nike

我尝试显示不同问题的列表。每个问题都有自己的表单输入。所以我写了一个指令来设置这些表单输入。但是在 input 标签中设置 ngModel 不会更新隔离范围。

我目前尝试过的:

<body ng-app="stepModule" ng-controller="ChallengeCtrl">
<div question step="step"></div>
</body>

还有 JS:

angular.module('stepModule', [])
.directive('question', function() {
return {
restrict: 'A',
replace: true,
scope: {
step: '='
},
template: "<form ng-submit=\"submit()\">\n
Step = {{step}}\n
<label for=\"question\">Question</label>\n
<input ngModel=\"step.question\" ng-required=\"true\" name=\"question\" />\n
<label for=\"answer\">Answer</label>\n
<input ngModel=\"step.answer\" ng-required=\"true\" name=\"answer\" />\n
<input type=\"submit\" value=\"Save (extract out, should save automatically)\" />\n
</form>",
controller: [
"$scope", "$element", "$attrs", function($scope, $element, $attrs) {
$scope.submit = function() {
console.log($scope.step.question);
console.log($scope.step.answer);
};
}
]
};
});

此处 console.log 将输出 $scope.step 的原始内容,而不是新值。 Here a Plunker表现出这种行为。

有没有办法让 ngModel 使用指令作用域?或者我只是错过了一些东西/滥用了 AngularJS(这至少不会让我感到惊讶......)

最佳答案

ngModel的属性语法是ng-model,即ng-model=\"step.answer\"

关于javascript - 具有隔离范围 : How to set ngModel to the correct scope? 的指令中的表单输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17852658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com