Angularjs 动态地将属性、行为或函数添加到angular.js控制器的$scope中

Angularjs 动态地将属性、行为或函数添加到angular.js控制器的$scope中,angularjs,angularjs-scope,Angularjs,Angularjs Scope,我喜欢尽可能地把代码分解成小块。因此,我正在研究是否可以将控制器的$scope属性和函数/行为与控制器的大括号(scope)分开,并将其写在myApp作用域以外的任何地方(可能在不同的*.js文件中,只需将其包含在HTML文件中) 我的代码示例如下所示: var myApp = angular.module('myApp.controllers', []); myApp.controller('login', ['$scope', function ($scope) { $scope.

我喜欢尽可能地把代码分解成小块。因此,我正在研究是否可以将控制器的$scope属性和函数/行为与控制器的大括号(scope)分开,并将其写在myApp作用域以外的任何地方(可能在不同的*.js文件中,只需将其包含在HTML文件中)

我的代码示例如下所示:

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

myApp.controller('login', ['$scope', function ($scope) {
    $scope.title = data.title;
    $scope.lab = data.lab;

//Following is the set of code that i will use to create the login validation logic. 
//I plan to put these in a new function/behavior in this same $scope.
//BUT i want these lines of code to lie outside this controller function braces.
    if ($scope.email== l.email && $scope.password== l.password) {
        $scope.Loginstatus = false;
    } else {
        $scope.Loginstatus = true;
    }
}]);
//Now, I know the following is going to be too crude/dirty/illogical but I am trying to explain what is in my mind. 
//Please forgive :) 

    myApp.controller.login.$scope.validate = function($scope){
        if ($scope.email== l.email && $scope.password== l.password) {
            $scope.Loginstatus = false;
        } else {
            $scope.Loginstatus = true;
        }
    };
我所期待的是这样的:

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

myApp.controller('login', ['$scope', function ($scope) {
    $scope.title = data.title;
    $scope.lab = data.lab;

//Following is the set of code that i will use to create the login validation logic. 
//I plan to put these in a new function/behavior in this same $scope.
//BUT i want these lines of code to lie outside this controller function braces.
    if ($scope.email== l.email && $scope.password== l.password) {
        $scope.Loginstatus = false;
    } else {
        $scope.Loginstatus = true;
    }
}]);
//Now, I know the following is going to be too crude/dirty/illogical but I am trying to explain what is in my mind. 
//Please forgive :) 

    myApp.controller.login.$scope.validate = function($scope){
        if ($scope.email== l.email && $scope.password== l.password) {
            $scope.Loginstatus = false;
        } else {
            $scope.Loginstatus = true;
        }
    };
请告知

谢谢你试试这个

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

myApp.controller('login', ['$scope', function ($scope) {
    $scope.title = data.title;
    $scope.lab = data.lab;

//Following is the set of code that i will use to create the login validation logic. 
//I plan to put these in a new function/behavior in this same $scope.
//BUT i want these lines of code to lie outside this controller function braces.
    $scope.login = validater({name:$scope.name, password:$scope.password});
}]);

var validater = function(credentials) {
    if (credentials.email== l.email && credentials.password== l.password) {
        return false;
    } else {
        return true;
    }
}

将您的应用程序拆分为更小的模块对于可读性和维护来说总是有利可图的

要快速了解angularjs项目是如何编写的,请查看AngularSeed项目:

通常,定义范围的方法和变量应该在范围本身中完成

在极少数情况下,您需要在控制器之外修改作用域属性:

在这种情况下,您可以将作用域置于控制器之外并使用它

var scope = angular.element(document.getelementById('el')).scope()
scope.$apply(function(){
    // do whatever with scope

    scope.validate = function() {
        if (scope.email== l.email && scope.password== l.password) {
            scope.Loginstatus = false;
        } else {
            scope.Loginstatus = true;
        }
    };

});

这个问题非常广泛,所以我投票决定结束,请阅读或类似的内容:)太好了!我在找这行的东西:)谢谢