【问题标题】:Using ng-model on two inputs, same value在两个输入上使用 ng-model,相同的值
【发布时间】:2015-01-29 16:10:17
【问题描述】:

我想在接收其他输入值的输入上使用 ng-model ...

我的代码不起作用,我不明白为什么,可以将 ng-model 设置为具有 Angular 值的输入吗?

我的代码

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

BottomApp.controller('SeoArticle', ['$scope', function($scope) {
  $scope.seoTitle = document.getElementById('title').value;
  $scope.createSeoUrl = function(string){
    var string;
    string = string.replace(/'+/g, '');
    string = string.replace(/"+/g, '');
    return string.replace(/\s+/g, '-');
  };
}]);
<div ng-controller="SeoArticle">
<input type="text" id="title" name="article[title]" class="form-control" placeholder="title" ng-model="seoTitle">

<input type="text" name="article[seo_title]" value="{{seoTitle2}}">
<input type="text" name="article[seo_url]" value="{{seoUrl2}}">

<div class="ui-block-title"><h5>{{seoTitle}}</h5></div>

<input type="text" id="title" class="form-control" placeholder="title" value="{{seoTitle}}" ng-model="seoTitle2">
<input type="text" id="seo_url" class="form-control" placeholder="seo_url" value="{{createSeoUrl(seoTitle)}}" ng-model="seoUrl2">

<div class="panel">
  <div class="seo-overview">
    <p class="seo-overview-title">{{seoTitle}}</p>
    <p class="seo-overview-url">{{createSeoUrl(seoTitle)}}</p>
  </div>
</div>
</div>

【问题讨论】:

  • 我建议为这个问题构建一个简单的示例,以说明您所询问的概念。
  • 我注意到的第一件事是 createSeoUrl 有一个名为“string”的参数,其中声明了一个名为“string”的变量。
  • 是的,我改了,但这不是问题...

标签: angularjs


【解决方案1】:

这是一个有效的Plunker

我已将 ng-app 调用添加到 body 标签中

<body ng-app="BottomApp">

并从 createSeoUrl 中删除了字符串变量声明。

编辑:我认为你不能在 DOM 中做到这一点。你应该使用观察者。查看更新的 Plunker。

$scope.$watch("seoTitle", function(newValue) {
    $scope.seoTitle2 = newValue;
    $scope.seoUrl2 = $scope.createSeoUrl(newValue);
})

<input type="text" id="title" class="form-control" placeholder="title" ng-model="seoTitle2">
<input type="text" id="seo_url" class="form-control" placeholder="seo_url" ng-model="seoUrl2">

【讨论】:

  • 不起作用,我的输入 name="article[seo_title]name="article[seo_url] 必须填写...
  • ng-model="seoTitle2" 输入的值应该是ng-model="seoTitle" 输入的值,name="article[seo_title]" 输入的值应该是ng-model="seoTitle2" 输入的值。 ..
猜你喜欢
  • 2016-11-29
  • 2019-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-30
  • 1970-01-01
  • 2016-06-17
  • 1970-01-01
相关资源
最近更新 更多