[英]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.