我只是想澄清一些有关 Angular 的事情。一个项目被移交给我,我注意到一些关于它的实现方式、没有范围等。如果在 AngularJS 中这个结构是什么实现或者你怎么称呼这个结构,谁能解释一下下面的代码吗?我明白这个逻辑,只是想知道我是否需要继续这样。关于作用域与变量,你能说些什么?或者如果这不是一个好的做法,请您纠正。使用标准 JavaScript 是一种好的做法吗?为什么?
使用标准js
var vm = this;
vm.title = 'some title';
vm.saveData = function(){ ... } ;
return vm;
app.controller('testCtrl', function ($scope, $http, $controller, JFactory, Notification, $timeout, BtnLoad) {
var me = this;
me.current_page = "test page"
me.ladda = {}
me.base_url = BASE_URL
me.record = {
"is_applicant": true
}
me.create = function () {
var notify = true;
var url = BASE_URL + "/register/";
var params = me.record;
$http.post(url, params)
.then(function (response) {
if (response.status != "200") {
return
}
me.record = { "is_applicant": true, }
if (notify) { Notification.success(response.data) }
BtnLoad.spin.stop()
console.log("Endpoint :", url, (me.current_page) , response.status)
JFactory.closeModal('.modal')
})
.catch(function (response) {
BtnLoad.spin.stop()
if (notify) { Notification.error(response.data) }
})
}
最佳答案
我将其称为:“遵循 John Papa Angular 1 风格指南”。
他建议使用此语法而不是 $scope
语法。
如果整个应用程序是按照他的指南编写的,我强烈建议您阅读他的指南。
如果没有,我仍然强烈建议您阅读他的指南,因为即使您不同意本指南中的所有内容,它仍然可以为 Angular JS 提供一些非常有用的指导。
https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md
关于javascript - 您如何称呼 Angular 中的这种结构或编码方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54143617/