繁体   English   中英

我如何在angular js模板中传递范围

[英]How can i pass scope in template in angular js

我有一个BaseController具有我所有其他控制器继承的通用功能。

控制器就是这样

function BaseController () {

    this.defaultFilters = {};

    this.doStuff = function ($scope) {
        $scope.myobj.value = 1;
        this.otherfunction();
    };

我像这样在控制器中继承了它

BaseController.call($scope);

现在在我的东西函数中,我需要传递$ scope,因为myobj仅在此处可见。

现在我想知道如何在模板中传递它,因为当某些按钮单击时我想调用该函数

ng-click="doStuff(scope)"

您与控制器的范围相关联的所有内容,因此您只需将范围与某个变量相关联,我想就可以完成工作。

像这样的东西:

    app.controller(function($scope) {
        $scope.scope = $scope;

    });

但是,如果您采用某种标准方法,则建议将这些通用功能移至某些服务中,然后将此服务注入每个控制器并在视图中使用它。

像这样的东西:

    app.service("constantService", function() {

        this.data = {}; // This will represent your common data.

        this.commonFunction = function() {

        };

    });


    app.controller(function() {
        $scope.constantService = constantService;


        // You can now use $scope.constantService.data as your reference for data, and then can copy it to some local $scope variable whenever required.
    });


    ng-click="constantService.commonFunction()"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM