简体   繁体   English

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

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

I have one BaseController with common functions which my all other controllers inherit . 我有一个BaseController具有我所有其他控制器继承的通用功能。

The controller is like this 控制器就是这样

function BaseController () {

    this.defaultFilters = {};

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

I inherit that in my controller like this 我像这样在控制器中继承了它

BaseController.call($scope);

Now in my do stuff function i need to pass $scope because myobj is only visible there. 现在在我的东西函数中,我需要传递$ scope,因为myobj仅在此处可见。

Now i want to know that how can i pass that in my template because i want to call that function when some click on some button 现在我想知道如何在模板中传递它,因为当某些按钮单击时我想调用该函数

ng-click="doStuff(scope)"

Everything that you associate with your controller's scope, so you just associate your scope with some variable and i guess that will do the job. 您与控制器的范围相关联的所有内容,因此您只需将范围与某个变量相关联,我想就可以完成工作。

Something like this : 像这样的东西:

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

    });

But if you go by some standard approach, i suggest moving these common functions inside some service, injecting this service into each controller and using it in the views. 但是,如果您采用某种标准方法,则建议将这些通用功能移至某些服务中,然后将此服务注入每个控制器并在视图中使用它。

Something like this : 像这样的东西:

    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.

相关问题 如何将$ scope传递给angular.js中的服务? - How can I pass $scope to a service in angular.js? Angular JS:如何在指令本地范围内设置可以在模板中使用的属性? - Angular JS: How do I set a property on directive local scope that i can use in the template? Angular / JS:如何将范围变量传递给javascript函数? - Angular/JS: How do I pass a scope variable to a javascript function? 如何将$ scope变量传递到自定义指令的字符串模板中? - How can I pass $scope variables into string template of custom directive? 如何在Angular中传递此回调并更新$ scope? - How can I pass this callback in Angular and update $scope? 如何在angular.js中将范围添加到http控制器? - How can i add a scope to a http controller in angular.js? 如何将预先生成的数组按角度传递给模板中的组件? - How can I pass pre generated array to component in template in angular? 我如何将数据从Django模板传递到Angle 4? - How can i pass data from django template to angular 4? 嵌套模板上的Angular js范围 - Angular js scope on nested template 如何将文本框的内容作为输入参数而不是 $scope 变量传递给 angular js? - How do I pass the contents of a textbox into angular js as an input parameter, rather than a $scope variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM