简体   繁体   English

从没有ngOptions的指令中设置angular.js控制器变量

[英]Setting angular.js controller variable from within a directive without ngOptions

I want to set the selected option's text to a controller variable exactly like how ngModel does this for the value attribute of the selected option. 我想将所选选项的文本设置为控制器变量,就像ngModel如何对所选选项的value属性进行设置一样。

<div ng-controller="Subjects as ctrl">
  <select ng-model="ctrl.model.fieldId" text-model="ctrl.model.fieldName">
    <option value="1">yes</option>
    <option value="2">no</option>
  </select>
</div>

how can I set the ctrl.model.fieldName to yes for example if the first option was selected which is not a function from within the directive? 我如何将ctrl.model.fieldName设置为yes ,例如,如果选择的第一个选项不是指令中的函数? this directive have to work alongside ngModel. 该指令必须与ngModel一起使用。

I want a way without using ngOptions because I don't have any values array in my javascript, although I can parse the HTML and create one. 我想要一种不使用ngOptions的方法,因为我的javascript中没有任何值数组,尽管我可以解析HTML并创建一个。

This the directive so far: 到目前为止,该指令:

(function () {
    angular
        .module('app')
        .directive('TextModel', TextModel);
    function TextModel($compile) {
        return {
            restrict: 'A',
            link: function ($scope, elem, attr) {
                elem.on('change', selectChanged);
                $scope.$on('$destroy', function () {
                    elem.off('change', selectChanged);
                });
                function selectChanged() {
                    var selectedText = elem.find('option:selected').text();
                    //YAY! I have the selected option's text
                    console.log(selectedText);
                    //now how can I set ctrl.model.fieldName = selectedText ??
                }
            }
        };
    };

})();

You don't need another directive for that. 您不需要其他指令。

It will be better to bind your select options to {key : value} array, or any other objects array 最好将您的选择选项绑定到{key:value}数组或任何其他对象数组

See here : https://docs.angularjs.org/api/ng/directive/ngOptions 看到这里: https : //docs.angularjs.org/api/ng/directive/ngOptions

Then, you can bind your ng-model to the selected item. 然后,您可以将ng-model绑定到所选项目。

$scope.items = [
    {key : 1 , value : 'yes'},
    {key : 2 , value : 'no'}
]

<select ng-model="selectedItem" ng-options="item as item.value for item in items"></select>

Demo on Jsfiddle Jsfiddle上的演示

Updated: 更新:

If you want to use your own directive for this, you can use isolated scope like this: 如果要为此使用自己的指令,则可以使用如下所示的隔离范围:

function TextModel($compile) {
    return {
        restrict: 'A',
        scope : {
            'textModel' : '='
        },
        link: function (scope, elem, attr ) {
            elem.on('change', selectChanged);
            scope.$on('$destroy', function () {
                elem.off('change', selectChanged);
            });
            function selectChanged() {

                var selectedText = $(elem).find('option:selected').text();
                //YAY! I have the selected option's text

                //now how can I set ctrl.model.fieldName = selectedText ??
                scope.textModel = selectedText;
            }
        }
    };
};

BTW, your directive setter should be: 顺便说一句,您的指令设置器应为:

.directive('textModel', TextModel);

instead of : 代替 :

.directive('TextModel', TextModel);

Demo on Jsfiddle Jsfiddle上的演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM