在angularjs中使用'this'而不是$ scope失败 [英] using 'this' instead of $scope failed in angularjs
问题描述
我使用megaboiletplate.com对我的项目进行了洗烫.我下载了入门包,但遇到了一些问题.我已经将入门代码存储在仓库中
I used megaboiletplate.com for scalfoding my project. I downloaded a starter pack but I'm facing some issue. I've put the starter code to a repo
在此文件中,单击登录按钮时,第16行未触发.登录按钮具有这样的视图
in this file, line 16 is not triggered when I click the login button. The login button has the view like this
<form ng-submit="login()">
<legend>Log In</legend>
<div class="form-group">
<label for="email">Email</label>
<input type="email" name="email" id="email" placeholder="Email" class="form-control" ng-model="user.email" autofocus>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" name="password" id="password" placeholder="Password" class="form-control" ng-model="user.password">
</div>
<div class="form-group"><a href="/forgot"><strong>Forgot your password?</strong></a></div>
<button type="submit" class="btn btn-success">Log in</button>
</form>
怎么了?
推荐答案
请确保您的代码使用的是"controller as"语法.您可以在控制器中使用 this
,但必须在HTML中重新定义控制器.像:
make sure that your code is using the 'controller as' syntax. You can use this
in the controller but you must redefine the controller in the HTML. like:
<body ng-controller="mycontroller as ctrl">
然后您可以使用控制器功能,例如
then you can use the controller functions like
<form ng-submit="ctrl.login()">
https://docs.angularjs.org/api/ng/directive/ngController
如果在此处分配了控制器,则可能必须编辑路由.
you may have to edit your routing if your controller is assigned there.
.when('/login', {
templateUrl: 'partials/login.html',
controller: 'LoginCtrl',
resolve: { skipIfAuthenticated: skipIfAuthenticated }
})
将更改为:
.when('/login', {
templateUrl: 'partials/login.html',
controller: 'LoginCtrl',
controllerAs: 'ctrl',
resolve: { skipIfAuthenticated: skipIfAuthenticated }
})
在您的情况下,应在样板的app.js中找到
in your case this should be found in the app.js of the boilerplate
这篇关于在angularjs中使用'this'而不是$ scope失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!