简体   繁体   中英

How can i pass scope in template in angular js

I have one BaseController with common functions which my all other controllers inherit .

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.

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()"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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