【问题标题】:How to set ng-model variable from custom validation directive如何从自定义验证指令设置 ng-model 变量
【发布时间】:2018-06-28 10:39:23
【问题描述】:

如何从链接函数外的 angularjs 指令调用控制器作用域

$scope 此处为未知提供商:

app.directive('checkId', function($parse,$scope, $http){//here $scope is unknown provider
return{
    restrict:'A',
    link: function(scope, element, attrs){
        element.bind('change', function(){
            var filteredLength = $parse(attrs.filtered)(scope);
            console.log(filteredLength);
            var newFilteredArray =  scope.stateList.filter(function(values){
                return values.toUpperCase() == scope.TRDetail.memberNo.toUpperCase();
            });
            if(filteredLength == 0){
                console.log("if called");
                scope.TRDetail.memberNo = "";
            }else if(newFilteredArray.length == 0){
                scope.TRDetail.memberNo = "";
            };
                scope.$apply();
                thisIsCalled(scope.TRDetail.memberNo);
        });
    }
};

function thisIsCalled(someData){
    if(someData == ""){
        alert("NOT doing some $http srvices");
    }else{
            $http.post("SomeURL", someData).then(function(response, error){
                if(error){
                    alert("sorry no data found" + error.message);
                }else{
                    $scope.TRDetail.memberName = response.data;
                }
        }); 
    };
};
});

HTML:

<div class="form-group col-md-3">
      <label>MemberShip NO:<span ng-if="(stateList|filter:TRDetail.memberNo).length==0" style="color:red;">(nothing found)</span></label>
      <input list="idList" class="form-control" name="browser"
             ng-model="TRDetail.memberNo"
             filtered="(stateList|filter:TRDetail.memberNo).length"
             check-id>
      <datalist id="idList">
        <option ng-repeat="x in stateList">{{x}}</option>
      </datalist>  
    </div>
    <div class="form-group col-md-9">
      <label>Member Name</label>
      <input type="text" ng-disabled="true" 
             ng-model="TRDetail.memberName" class="form-control"
             placeholder="type name here..">
    </div>

我无法通过链接功能在外部更新TRDetail.memberName。 有什么帮助,怎么办?

【问题讨论】:

  • @georgeawg,我想使用thisIsCalled从链接函数传递一个变量;然后这个函数将更新 html 中的父范围。
  • @GuillaumeGeorges 建议的副本不适用于这种情况。

标签: angularjs angularjs-directive angularjs-scope angularjs-ng-model


【解决方案1】:

tl;博士;

使用ngModel.$setViewValue


当自定义指令与ng-model 指令结合使用时,该指令应使用ngModelController API:

app.directive('checkId', function($parse,$http){
return{
    require: 'ngModel',
    restrict:'A',
    link: function(scope, element, attrs, ngModel){
        ̶e̶l̶e̶m̶e̶n̶t̶.̶b̶i̶n̶d̶(̶'̶c̶h̶a̶n̶g̶e̶'̶,̶ ̶f̶u̶n̶c̶t̶i̶o̶n̶(̶)̶{̶
        ngModel.$viewChangeListeners.push(onChange);
        function onChange(){
            if (!ngModel.$modelValue) return;
            var filteredLength = $parse(attrs.filtered)(scope);
            console.log(filteredLength);
            var newFilteredArray =  scope.stateList.filter(function(values){
                ̶r̶e̶t̶u̶r̶n̶ ̶v̶a̶l̶u̶e̶s̶.̶t̶o̶U̶p̶p̶e̶r̶C̶a̶s̶e̶(̶)̶ ̶=̶=̶ ̶s̶c̶o̶p̶e̶.̶T̶R̶D̶e̶t̶a̶i̶l̶.̶m̶e̶m̶b̶e̶r̶N̶o̶.̶t̶o̶U̶p̶p̶e̶r̶C̶a̶s̶e̶(̶)̶;̶
                return values.toUpperCase() == ngModel.$modelValue.toUpperCase();
            });
            if(filteredLength == 0){
                console.log("if called");
                ̶s̶c̶o̶p̶e̶.̶T̶R̶D̶e̶t̶a̶i̶l̶.̶m̶e̶m̶b̶e̶r̶N̶o̶ ̶=̶ ̶"̶"̶;̶
                ngModel.$setViewValue("");
            }else if(newFilteredArray.length == 0){
                ̶s̶c̶o̶p̶e̶.̶T̶R̶D̶e̶t̶a̶i̶l̶.̶m̶e̶m̶b̶e̶r̶N̶o̶ ̶=̶ ̶"̶"̶;̶
                ngModel.$setViewValue("");
            };
            ̶s̶c̶o̶p̶e̶.̶$̶a̶p̶p̶l̶y̶(̶)̶;̶
            ̶t̶h̶i̶s̶I̶s̶C̶a̶l̶l̶e̶d̶(̶s̶c̶o̶p̶e̶.̶T̶R̶D̶e̶t̶a̶i̶l̶.̶m̶e̶m̶b̶e̶r̶N̶o̶)̶;̶
            thisIsCalled(ngModel.$modelValue);
        }
        function thisIsCalled(someData){
            if(someData == ""){
                alert("NOT doing some $http srvices");
            }else{
                $http.post("SomeURL", someData)
                  .then(function(response){
                     ngModel.$setViewValue(response.data);
                })
                  .catch(function(error) {
                     alert("sorry no data found" + error.status);
                }); 
            };
        }
    }
};

有关详细信息,请参阅

【讨论】:

    【解决方案2】:

    您的函数 thisIsCalled(someData) 只是不在您的 Angular 应用程序中。您应该通过指令的控制器访问 $scope 元素:

    angular.directive('checkId', function($parse, $scope, $http) {
        return {
            restrict: 'A',
            link: //your link stuff
            controller: myController
        };
    });
    
    function myController($scope) {
        //here you can access the scope
    }
    

    【讨论】:

    • myController 替换为thisIsCalled 随意
    • 我仍然无法通过传递一些变量来调用函数并更新主控制器作用域,因为一个变量将从链接函数thisIscalled 函数传递并更新父作用域。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多