javascript - 使用 AngularJS 中的指令将对象存储在 ngModel 中

标签 javascript angularjs model angularjs-directive angularjs-ng-model

我已经使用 AngularJS 工作了几个星期了,我不明白 AngularJS 设计者在设计 ngModelController 中的 $viewValue 和 $modelValue 功能时的想法。 。代码示例:

index.html:

<!DOCTYPE html>
<html>

  <head>
    <script data-require="angular.js@*" data-semver="1.3.0-beta.5" src="https://code.angularjs.org/1.3.0-beta.18/angular.js"></script>
    <script src="script.js"></script>
  </head>

  <body ng-app="PlunkerApp" ng-controller="mainController">
    <listfield ng-model="termList"></listfield>
  </body>

</html>

script.js:

var plunkerModule = angular.module('PlunkerApp', []);

plunkerModule.directive('listfield', function() {
  'use strict';
  var link = function(scope, element, attrs, ngModelController) {
    console.log('listfield.link():', scope);
    ngModelController.$parsers.push(function(value) {
      console.log('listfield.model.parser:', value);
      return value ? value.join(', ') : undefined;

    });
    ngModelController.$formatters.push(function(value) {
      console.log('listfield.model.formatter:', value);
      return value ? value.split(/,\s*/) : undefined;
    });
  }
  return {
    restrict: 'E',
    link: link,
    require: 'ngModel',
    scope: {
      ngModel: '='
    },
    template: '<input type="text" ng-model="ngModel">'
  };
});

plunkerModule.controller('mainController', function($scope) {
  $scope.termList = "";
  $scope.$watchCollection('termList', function(newValue, oldValue) {
    console.log('mainController.watch.list:', newValue);
  });
});

骗子链接: http://plnkr.co/edit/T5a8zEQuRyYWnpsZZV9W?p=preview

因此,在这个应用程序中,指令输入元素的值被写入全局范围,这工作得很好!我的问题是,我对“原始”字符串值不感兴趣,我想要由格式化程序生成的数组,但输入元素仍应显示字符串值。

我怎样才能做到这一点?

期待您的答复。

最佳答案

这里的问题是你的 <input>和你的<listfield>标签有一个 ngModel,这会让人混淆何时调用哪个。您可以使用 replace: true您的指令的选项可删除 <listfield>标记并仅适用于 <input> ,像这样:

var plunkerModule = angular.module('PlunkerApp', []);

plunkerModule.directive('listfield', function() {
  'use strict';
  var link = function(scope, element, attrs, ngModelController) {
    console.log('listfield.link():', scope);
    // Your formatters and parsers seemed to be the other way around
    // The formatter transforms Model -> View
    // Whereas the parser transforms View -> Model
    ngModelController.$formatters.push(function(value) {
      console.log('listfield.model.formatter:', value);
      return value ? value.join(', ') : undefined;

    });
    ngModelController.$parsers.push(function(value) {
      console.log('listfield.model.parser:', value);
      return value ? value.split(/,\s*/) : undefined;
    });
  }
  return {
    restrict: 'E',
    link: link,
    require: 'ngModel',
    replace: true, // Removes the <listfield> tag
    scope: {
      model: '='
    },
    template: '<input type="text" ng-model="model">'
  };
});

plunkerModule.controller('mainController', function($scope, $timeout) {
  $scope.termList = [1,2,3]
  $scope.$watchCollection('termList', function(newValue, oldValue) {
    console.log('mainController.watch.list:', newValue);
  });
  $timeout(function changeTermList() { $scope.termList = [4,5,6]}, 2000)
  // This is to demonstrate the binding used via the isolate scope(=)
});

以及相关的 HTML:

  <body ng-app="PlunkerApp" ng-controller="mainController">
    <listfield model="termList"></listfield>
  </body>

演示:http://plnkr.co/edit/G0tlC9qhW8AHa58yVSgj?p=preview

关于javascript - 使用 AngularJS 中的指令将对象存储在 ngModel 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25380134/

相关文章:

javascript - React Router 重定向错误 404

javascript - 发布带有控制字符的表单

javascript - 使用 angular/karma/jasmine 模拟异步服务功能

model - 如何使用 AWS::ApiGateway::Model CloudFormation 保持架构属性有序

javascript - 如何使用 CSS 或 JS 让一些文本输入在输入中显示帮助图标?

javascript - 如何循环遍历对象数组并过滤掉同一小时内的项目

angularjs - Yeoman/Grunt/Angular 中的 dist 构建目标文件夹是什么?

javascript - 将类附加到元素angularjs后,ng-class可以设置超时吗?

c# - 最适合模型的位图类

ruby-on-rails - 如何使用 Ruby on Rails 将数据从 Controller 传递到模型?