gpt4 book ai didi

javascript - 双向绑定(bind)指令+ Controller

转载 作者:行者123 更新时间:2023-11-27 22:55:17 25 4
gpt4 key购买 nike

我在 DOM 中动态添加一个文本字段(来自指令链接函数)并希望获取输入的值并将其推送到 Controller 范围对象,但它总是给我未定义,下面是我的代码:

<div class="input-group">
<span class="fieldIcon input-group-addon"><i class="fa fa-sticky-note" aria-hidden="true"></i></span>
<select name="addlist[]" multiple="multiple">
<option ng-repeat="options in optionList">{{options.label}}</option>
</select>
</div>
<script type="text/javascript">
angular.module('myapp')
.controller('AddContactController',[ '$scope', function ($scope) {
$scope.optionList = [{label: 'NewList'}];

$scope.addOption = function(optionList) {
console.log('List:', optionList); // its giving undefined
scope.optionList.push(optionList);
}

}])
.directive('optionList', ['$compile', function ($compile) {
return {
restrict: 'E',
templateUrl: '/templates/int_optionList.html',
controller: 'AddContactController',
link: function(scope, element, attrs) {
// Adding input field and on click of a button controllers addOption function should be called with the text field value
var addListField = '<input class="form-control" type="text" ng-modal="addList" name="addList" placeholder="Add new list...">'+
+'<button class="btn btn-default" type="button" ng-click="addOption()">';

addListField = $compile(addListField)(scope);
$(element).find('.multiselect-container').prepend(addListField);
}
}
}]);
</script>

现在在 addOption 函数中,我得到的 optionList 值未定义。

最佳答案

错误是人为的:)。在指令中将 ng-modal 替换为 ng-model

关于javascript - 双向绑定(bind)指令+ Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37674998/

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