【问题标题】:What do you call this structure or way of coding in Angular?在 Angular 中,您如何称呼这种结构或编码方式?
【发布时间】:2019-01-11 23:40:12
【问题描述】:

我只是想澄清一些关于 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) }
            })
    }

【问题讨论】:

    标签: javascript angularjs architecture structure


    【解决方案1】:

    我称之为:“遵循 John Papa Angular 1 样式指南”。

    他建议使用此语法而不是 $scope 语法。

    如果整个应用程序是按照他的指南编写的,我强烈建议您阅读他的指南。

    如果不是,我仍然强烈建议您阅读他的指南,因为即使您不同意本指南中的所有内容,它仍然可以为 Angular JS 提供一些非常有用的指导。

    https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md

    【讨论】:

    • 我会在这里指出,如果这段代码是通过一个缩小器运行的,那么函数参数可能会被缩短,并且这段代码会中断。
    • 在他的代码中是的。但是,如果遵循指南的建议,则可以避免此问题...github.com/johnpapa/angular-styleguide/blob/master/a1/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    相关资源
    最近更新 更多