Javascript AngularJS-遵循自定义指令中的作用域

Javascript AngularJS-遵循自定义指令中的作用域,javascript,angularjs,Javascript,Angularjs,我有自定义指令,希望将一些参数(对象)发送到此指令 在指令中,我有基于此参数之一的选择列表,我需要遵循此参数 我的指示: <custom-directive rows="users"> </custom-directive> 您可以使用链接而不是控制器来处理具有行的功能。设置行:“=”应已负责双向绑定 .directive('custom-directive', function () { return { restrict: 'E',

我有自定义指令,希望将一些参数(对象)发送到此指令

在指令中,我有基于此参数之一的选择列表,我需要遵循此参数

我的指示:

<custom-directive rows="users">

</custom-directive>

您可以使用链接而不是控制器来处理具有行的功能。设置行:“=”应已负责双向绑定

.directive('custom-directive', function () {
    return {
        restrict: 'E',
        replace: true,
        scope: {
           rows: '='
        },
        templateUrl: 'template.html',
        link: function(scope, element, attr) {
            console.log(scope.rows);
        };
    };
})
编辑:

将手表添加到需要对外部更改执行操作的值中

.directive('custom-directive', function () {
    return {
        restrict: 'E',
        replace: true,
        scope: {
           rows: '='
        },
        templateUrl: 'template.html',
        link: function(scope, element, attr) {
            scope.$watch('rows', function(oldValue, newValue) {
                console.log('This is the newValue : ' + newValue);
            };
        };
    };
})

您可以通过以下方式修改自定义指令以使用
ng型号

directive('customDirective', function() {
  return {
    // ...
    require: '?ngModel', // get a hold of NgModelController
    link: function(scope, element, attrs, ngModel) {
      if (!ngModel) return;              

      // Write data to the model
      // Call it when necessary
      scope.setValue = function(value) {
        ngModel.$setViewValue(value);
      }
    }
  };
});
可以使用该方法检索
ng模型
值的初始状态:

ngModel.$render = function() {
    var value = ngModel.$viewValue || [];
};

显示你的指令代码我已经编辑了这篇文章。实际上,我的指令几乎是空的。指令名称在html和指令创建中都是错误的…应该是。指令('customDirective',函数(){`&在html上它应该是
我已经为这个站点更改了它。一切都正常。我尝试使用链接-它不进行更改。实际上我需要在这个范围的模板中始终具有实际值-在
template.html
中。当外部的内容发生更改(
用户
变量)时此更改必须出现在
template.html
中。我更新了我的答案。我的错误在于第一次忽略了这一点。如果您需要基于指令之外的操作更新指令值时发生行为,您将需要一个手表。实际上……我已删除了所有内容并再次执行了此操作。它现在可以工作,但我不知道是什么原因e.区别。但它是有效的:)
directive('customDirective', function() {
  return {
    // ...
    require: '?ngModel', // get a hold of NgModelController
    link: function(scope, element, attrs, ngModel) {
      if (!ngModel) return;              

      // Write data to the model
      // Call it when necessary
      scope.setValue = function(value) {
        ngModel.$setViewValue(value);
      }
    }
  };
});
ngModel.$render = function() {
    var value = ngModel.$viewValue || [];
};