Angularjs 当我需要ngModel controller时,我如何访问model controller的属性

Angularjs 当我需要ngModel controller时,我如何访问model controller的属性,angularjs,angularjs-directive,angularjs-ng-repeat,angularjs-controller,Angularjs,Angularjs Directive,Angularjs Ng Repeat,Angularjs Controller,我正在使用ng repeat并使用它设置一个模型,如下所示 <div ng-repeat="thing in things" ng-model="thing" my-directive> {{thing.name}} </div> 我的问题是如何访问模型中的值?我尝试了model.$modelValue.name,但没有成功。如果您想在范围值中绑定,那么可以在独立的值中使用“=”。这将出现在指令的范围内。要阅读ng model指令,可以使用=ngModel:

我正在使用
ng repeat
并使用它设置一个模型,如下所示

<div ng-repeat="thing in things" ng-model="thing"  my-directive>
    {{thing.name}}
</div>

我的问题是如何访问模型中的值?我尝试了
model.$modelValue.name
,但没有成功。

如果您想在范围值中绑定,那么可以在独立的值中使用“=”。这将出现在指令的范围内。要阅读
ng model
指令,可以使用
=ngModel

.directive("myDirective", function () {
    return {
        require: 'ngModel',
        link: function(scope, lElement, attrs, model) {

         console.log(attrs.ngModel); // will log "thing"
        }
    }
})
.directive("myDirective", function () {
    return {
        scope: {
            model: '=ngModel'
        }
        link: function(scope) {

         console.log(scope.model.name); // will log "thing"
        }
    }
});

如果您想绑定一个范围值,那么可以在一个独立的值中使用“=”。这将出现在指令的范围内。要阅读
ng model
指令,可以使用
=ngModel

.directive("myDirective", function () {
    return {
        scope: {
            model: '=ngModel'
        }
        link: function(scope) {

         console.log(scope.model.name); // will log "thing"
        }
    }
});

如果指令没有独立或子作用域,则可以执行以下操作:

.directive('someDirective', function() {
    return {
        require: ['^ngModel'],
        link: function(scope, element, attrs, ctrls) {
            var ngModelCtrl = ctrls[0];
            var someVal;
            // you have to implement $render method before you can get $viewValue
            ngModelCtrl.$render = function() {
                someVal = ngModelCtrl.$viewValue;
            };
            // and to change ngModel use $setViewValue
                    // if doing it in event handler then scope needs to be applied
            element.on('click', function() {
                var val = 'something';
                scope.$apply(function() {
                    ngModelCtrl.$setViewValue(val);
                });
            });
        }
    }
});

如果指令没有独立或子作用域,则可以执行以下操作:

.directive('someDirective', function() {
    return {
        require: ['^ngModel'],
        link: function(scope, element, attrs, ctrls) {
            var ngModelCtrl = ctrls[0];
            var someVal;
            // you have to implement $render method before you can get $viewValue
            ngModelCtrl.$render = function() {
                someVal = ngModelCtrl.$viewValue;
            };
            // and to change ngModel use $setViewValue
                    // if doing it in event handler then scope needs to be applied
            element.on('click', function() {
                var val = 'something';
                scope.$apply(function() {
                    ngModelCtrl.$setViewValue(val);
                });
            });
        }
    }
});

为什么会被否决?为什么会被否决?