如何设置输入值

时间:2018-07-12 20:05:51

标签: javascript angularjs

我无法在输入中设置值:

<html ng-app="myApp" ng-controller="mycontroller as controlss">

<input type="text" class="form-name  ng-pristine ng-empty ng-valid-vin ng-invalid ng-invalid-required ng-valid-maxlength ng-touched" name="inputUser" ng-model="user.name" ng-required="toReq('user.name', data.assist)" required="required">

并尝试使用脚本:

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

test.controller('controlss', ['$scope', function ($scope)
{

$scope.user ={ name: 'MAX'}; 

}]);

我在做什么错?  你有什么主意吗?

2 个答案:

答案 0 :(得分:0)

ng模型输入user.name缺少控制器名称,在您的情况下为controlss

<html ng-app="myApp" ng-controller="mycontroller as controlss">

<input type="text" class="form-name" name="inputUser" ng-model="controlss.user.name" ng-required="toReq('user.name', data.assist)" required="required">

angularjs文档中有一个很好的例子: https://docs.angularjs.org/api/ng/directive/ngController

答案 1 :(得分:0)

您应该将代码修改为这样:

<html ng-app="myApp" ng-controller="controlss">
<input type="text" class="form-name  ng-pristine ng-empty ng-valid-vin ng-invalid ng-invalid-required ng-valid-maxlength ng-touched" name="inputUser" ng-model="user.name" ng-required="toReq('user.name', data.assist)" required="required">