没有隔离范围的情况下如何从指令到控制器获取值

时间:2019-02-22 09:45:41

标签: angularjs asp.net-mvc c#-4.0

我有一个局部视图,在其中我使用了一个element指令。我想在没有隔离范围的控制器中使用该模型。 如果我使用隔离范围,则指令中未定义多重选择元素 我正在使用MVC Partial视图,Angular JS基本版本。 以下是我的快照代码:

“ _ abc.cshtml”

<div class="modal-body formpopupbody"> 

            <multiselect name="selection" multiple="true" ng-model="Type" 
                         ng-required="true" class="dropStyle" options="c.Value for c in TypeList" change="selected()">    
            </multiselect>      
</div>

MultiSelect.js

.directive('multiselect', ['$parse', '$document', '$compile', 'optionParser',

    function ($parse, $document, $compile, optionParser) {
        return {
            restrict: 'E',
            require: 'ngModel',            
            link: function (originalScope, element, attrs, modelCtrl) {

                var exp = attrs.options,
                  parsedResult = optionParser.parse(exp),
                  isMultiple = attrs.multiple ? true : false,
                  required = false,
                  scope = originalScope$new(),
                  changeHandler = attrs.change || anguler.noop;


                var popUpEl = angular.element('<multiselect-popup></multiselect-popup>');

                //required validator
                if (attrs.required || attrs.ngRequired) {
                    required = true;
                }


                //watch single/multiple state for dynamically change single to multiple
                scope.$watch(function () {
                    return $parse(attrs.multiple)(originalScope);
                }, function (newVal) {
                    isMultiple = newVal || false;
                });

                //watch option changes for options that are populated dynamically
                scope.$watch(function () {
                    return parsedResult.source(originalScope);
                }, function (newVal) {
                    if (angular.isDefined(newVal))
                        parseModel();
                });

                //watch model change
                scope.$watch(function () {
                    return modelCtrl.$modelValue;
                }, function (newVal, oldVal) {
                    //when directive initialize, newVal usually undefined. Also, if model value already set in the controller
                    //for preselected list then we need to mark checked in our scope item. But we don't want to do this every time
                    //model changes. We need to do this only if it is done outside directive scope, from controller, for example.
                    if (angular.isDefined(newVal)) {
                        markChecked(newVal);
                        scope.$eval(changeHandler);
                    }
                    getHeaderText();
                    modelCtrl.$setValidity('required', scope.valid());
                }, true);


                element.append($compile(popUpEl)(scope));

                scope.valid = function validModel() {
                    if (!required) return true;
                    var value = modelCtrl.$modelValue;
                    return (angular.isArray(value) && value.length > 0) || (!angular.isArray(value) && value != null);
                };
                function selectMultiple(item) {
                    item.checked = !item.checked;
                    setModelValue(true);
                }
                function setModelValue(isMultiple) {
                    var value;                   
                        value = [];
                        angular.forEach(scope.items, function (item) {
                            if (item.checked) value.push(item.model);
                        })                    
                    if (value.length == 0)
                        value = null;

                    var val = modelCtrl.$setViewValue(value);
                    //I want this val value in controller
                }
            }
        };
    }])

这是我的控制器

!
function () {
    "use strict";
    function abcCtrl($rootScope, $scope, $http, $window, $timeout, toastr, gridConstant, regEx, $uibModal) {
        $scope.$on('$viewContentLoaded', function () {
            $scope.Type = [];
            $scope.TypeList = [];

        });  
        $scope.SaveData = function () {

            try {               
               //here i want access directive value
        }        
    };
    abcCtrl.$inject = ["$rootScope", "$scope", "$http", "$window", "$timeout", "toastr", "gridConstant", "regEx", "$uibModal"];
    angular.module("app").controller("abcCtrl", abcCtrl);
}();

有人知道如何获得这一价值吗?

0 个答案:

没有答案
相关问题