我应该给我的指令赋予什么范围,以便输入显示初始值"Toto"?我不想占用范围:是的
HTML代码:
<!doctype html>
<html ng-app="plunker" >
<head>
<meta charset="utf-8">
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<input customattr type = "text" ng-model="value.name" />
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
JS代码:
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.value = {"name":"Toto", "id":1};
});
app.directive('customattr', function () {
return {
restrict: 'A',
scope: {
},
link: function (scope, element, attrs) {
}
};
});
Run Code Online (Sandbox Code Playgroud)
Plunker:http://plnkr.co/edit/JxWElWhTeBbNpFHS0wYT
我正在尝试在用户按Enter键时提交登录表单.单击"登录"按钮时,表单完全正常,但按Enter键不起作用,此外,会导致奇怪的行为:
ng-submit未被执行这是我的标记:
<form ng-submit="login()" class="login-form">
<div class="alert alert-danger" ng-class="{ 'display-hide': !showError }">
<button class="close" data-close="alert"></button>
<span> Login failed </span>
</div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Username</label>
<input class="form-control form-control-solid placeholder-no-fix" type="text" autocomplete="off" placeholder="Username" name="username" ng-model="username"/>
</div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Password</label>
<input class="form-control form-control-solid placeholder-no-fix" type="password" autocomplete="off" placeholder="Password" name="password" ng-model="password"/>
</div>
<div class="form-actions">
<button class="btn btn-success uppercase">Login</button>
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
请注意,我试图取代<button...>与<input type="submit"...>没有成功要么.
具有login() - 函数的相应控制器如下所示:
.controller('LoginCtrl', ['$rootScope', '$scope', ..., function LoginController ($rootScope, …Run Code Online (Sandbox Code Playgroud)