使用AngularJS,如果我设置一个简单的输入文本框值为如下所示的“bob”。如果添加了ng-model属性,则不显示该值。

    <input type="text"
           id="rootFolder"
           ng-model="rootFolders"
           disabled="disabled"
           value="Bob"
           size="40"/>

有人知道一种简单的方法来默认这个输入并保留ng-model吗?我尝试使用默认值ng-bind,但这似乎也不起作用。


当前回答

嗨,你可以尝试下面的方法与初始化的模型。

这里你可以用两种方式初始化ng-model的文本框

-使用ng-init

-在js中使用$scope

<!doctype html>
 <html >
 <head>
        <title>Angular js initalize with ng-init and scope</title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
</head>
<body ng-app="app" >
    <h3>Initialize value with ng-init</h3>
    <!-- Initlialize model values with ng-init -->
    <div ng-init="user={fullname:'Bhaskar Bhatt',email:'bhatt.bhaskar88@gmail.com',address:'Ahmedabad'};">
        Name : <input type="text" ng-model="user.fullname" /><br/>
        Email : <input type="text" ng-model="user.email" /><br/>
        Address:<input type="text" ng-model="user.address" /><br/>

    </div>  
    <!-- initialize with js controller scope -->
    <h3>Initialize with js controller</h3>

    <div  ng-controller="alpha">
        Age:<input type="text" name="age" ng-model="user.age" /><br/>
        Experience : <input type="text" name="experience" ng-model="user.exp" /><br/>
        Skills : <input type="text" name="skills" ng-model="user.skills" /><br/>
    </div>  

</body> 
<script type="text/javascript">
        angular.module("app",[])
        .controller("alpha",function($scope){
            $scope.user={};
            $scope.user.age=27;
            $scope.user.exp="4+ years";
            $scope.user.skills="Php,javascript,Jquery,Ajax,Mysql";
        });

     </script>
 </html>                

其他回答

Vojta描述了“Angular的方式”,但如果你真的需要让这个工作,@urbanek最近发布了一个使用ng-init的变通方法:

<input type="text" ng-model="rootFolders" ng-init="rootFolders='Bob'" value="Bob">

https://groups.google.com/d/msg/angular/Hn3eztNHFXw/wk3HyOl9fhcJ

问题是您必须将ng-model设置为父元素,设置为您想要设置ng-value/value的位置。 正如Angular提到的:

它主要用于输入[radio]和选项元素,因此当元素被选中时,该元素(或其选择父元素)的ngModel被设置为绑定值。

这是一个执行的代码:

<div class="col-xs-12 select-checkbox" >
<label style="width: 18em;" ng-model="vm.settingsObj.MarketPeers">
  <input name="radioClick" type="radio"  ng-click="vm.setPeerGrp('market');" 
         ng-value="vm.settingsObj.MarketPeers" 
         style="position:absolute;margin-left: 9px;">
  <div style="margin-left: 35px;color: #717171e8;border-bottom: 0.5px solid #e2e2e2;padding-bottom: 2%;">Hello World</div>
</label>
</div>

注意:在上面的例子中,我已经有了对ng-model和值的JSON响应,我只是将另一个属性添加到JS对象作为“MarketPeers”。因此,模型和值可能取决于需要,但我认为这个过程会有帮助,有ng-model和值,但不把它们放在同一个元素上。

嗨,你可以尝试下面的方法与初始化的模型。

这里你可以用两种方式初始化ng-model的文本框

-使用ng-init

-在js中使用$scope

<!doctype html>
 <html >
 <head>
        <title>Angular js initalize with ng-init and scope</title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
</head>
<body ng-app="app" >
    <h3>Initialize value with ng-init</h3>
    <!-- Initlialize model values with ng-init -->
    <div ng-init="user={fullname:'Bhaskar Bhatt',email:'bhatt.bhaskar88@gmail.com',address:'Ahmedabad'};">
        Name : <input type="text" ng-model="user.fullname" /><br/>
        Email : <input type="text" ng-model="user.email" /><br/>
        Address:<input type="text" ng-model="user.address" /><br/>

    </div>  
    <!-- initialize with js controller scope -->
    <h3>Initialize with js controller</h3>

    <div  ng-controller="alpha">
        Age:<input type="text" name="age" ng-model="user.age" /><br/>
        Experience : <input type="text" name="experience" ng-model="user.exp" /><br/>
        Skills : <input type="text" name="skills" ng-model="user.skills" /><br/>
    </div>  

</body> 
<script type="text/javascript">
        angular.module("app",[])
        .controller("alpha",function($scope){
            $scope.user={};
            $scope.user.age=27;
            $scope.user.exp="4+ years";
            $scope.user.skills="Php,javascript,Jquery,Ajax,Mysql";
        });

     </script>
 </html>                

我也有类似的问题。我不能使用value="something"来显示和编辑。 我必须在<input>中使用下面的命令,同时声明ng模型。

[(ngModel)]=userDataToPass.pinCode

其中,我有对象userDataToPass中的数据列表,我需要显示和编辑的项目是pinCode。

同样的,我参考了这个YouTube视频

如果你使用AngularJs的ngModel指令,记住value属性的值不会绑定在ngModel字段上。你必须自己初始化它,最好的方法是

<input type="text"
       id="rootFolder"
       ng-init="rootFolders = 'Bob'"
       ng-model="rootFolders"
       disabled="disabled"
       value="Bob"
       size="40"/>