2014-12-01 30 views
0

我想对接收的其他输入的值输入使用NG-模型...使用NG-模式,相同的值

我的代码不工作,我不理解为什么,是否可以将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>

+0

我建议建立为说明这一点,你问这个概念的问题一个简单的示例。 – Kolban 2014-12-01 15:54:42

+1

我注意到的冷杉事件是createSeoUrl有一个名为'string'的参数,里面有一个名为'string'的变量。 – 2014-12-01 15:58:07

+0

是的我改变了它,但这不是问题... – tonymx227 2014-12-01 16:07:02

回答

2

这里的工作Plunker

我已经加入了NG-应用程序调用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"> 
+0

不工作,我的输入'name =“文章[seo_title]'和'name =”文章[seo_url]'必须填写... – tonymx227 2014-12-01 16:06:14

+0

值ng-model =“seoTitle2”的输入的值应该是具有'ng-model =“seoTitle”'的输入值,'name =“article [seo_title]”的输入值应该是用ng-model =“seoTitle2”输入的值......“...... – tonymx227 2014-12-01 16:17:32

+0

好吧,它可以工作,谢谢。 ;) – tonymx227 2014-12-02 07:24:50