Angularjs$scope和此函数
Angularjs $scope and this functions
我正在angularjs:-中开发一个小型应用程序
我正在删除一个联系人。一切都很好,但this.openModal()会抛出一个未定义的错误,即使它是在同一个JS中定义的。
关于如何将this和$scope一起使用,有一些困惑。有人能帮忙吗?
$scope.deleteContact = function ()
{
var delNo = $scope.contactNumber;
var delName = $scope.contactName;
var person = {};
person['phone'] = delNo;
...
...
$timeout(function(){
delete $scope.contacts[person['phone']];
});
$scope.activeChats={};
$scope.showContactName = false;
this.openModal();
console.log("Delete success");
}
编辑:
这个.openModal是一个我定义如下的函数
this.openModal = function (input) {
this.modalOpen = !this.modalOpen;
// RESET
this.showNewMessage = false;
this.showEditGroup = false;
this.showAddContact = false;
this.showPersonSettings = false;
this.showUserProfile = false;
if (input == "newmessage"){
this.showNewMessage = true;
} else if (input == "showEditGroup"){
this.showEditGroup = true;
} else if (input == "newcontact"){
this.showAddContact = true;
} else if (input == "userprofile"){
this.showUserProfile = true;
} else if (input == "usersettings"){
this.showPersonSettings = true;
}
}
您正在做什么还不完全清楚,但我想您在执行异步函数时遇到了一些上下文问题。尝试将$scope
分配给一个局部变量,在函数块中关闭它,并在asnyc函数块中使用该变量。
$scope.deleteContact = function ()
{
var person = {
phone: $scope.contactNumber
};
...
// save scope reference to local variable
var scope = $scope;
$timeout(function(){
// use saved scope
delete scope.contacts[person['phone']];
});
$scope.activeChats={};
$scope.showContactName = false;
this.openModal();
console.log("Delete success");
}
还有另一种方法可以在angular的代码中做一些类似的事情。这就是angular.bind
。在$timeout
中使用它。相似之处在于,您在执行时提供函数的上下文,因此this
就是您提供的内容。在下面的例子中,我提供$scope
作为异步函数的执行上下文,这是指使用this
:
$timeout(angular.bind($scope, function(){
// context (this) is actually $scope
delete this.contacts[person['phone']];
}));
在两种情况下使用$scope
或this
。使用绑定的第一个场景:
ng-controller="MyCtrl"
或者在路线上:
when('/color', { controller : 'MyCtrl' });
在这里,angular js希望您在控制器中包含$scope
服务,并为其附加可绑定属性:
angular.module('myModule')
.controller('MyCtrl', ['$scope', myCtrl]);
function myCtrl($scope) {
$scope.title = "Hello";
}
在第二个场景中,您定义了一个controllerAs
,angular将在这里看到控制器对象上的可绑定属性,控制器是一个类。我更喜欢这种方法,因为它看起来更干净。
ng-controller="MyCtrl as vm"
或者在路线上:
when('/color', { controller : 'MyCtrl', controllerAs: 'vm' });
控制器:
angular.module('myModule')
.controller('MyCtrl', [myCtrl]);
function myCtrl() {
this.title = "Hello";
// or to make sure that you don't get JS contexts mixed up
var vm = this;
vm.title = "Hello";
vm.openModal = function () {
this;// this refers to the current function scope
vm.title = "Modal Opened"; // this refers to the controller scope
// scope as in the JS function context.
}
}
在第二个场景中,绑定的页面将如下所示:
<h3>{{vm.title}}</h3>
使用"controllerAs"字符串作为用于访问属性的对象的位置。
因此,为了回答这个问题:对于如何将this和$scope一起使用有一些困惑
你通常会使用其中一个。在第二个场景中,只有在需要$watch
属性或使用其他特殊作用域函数时,才会注入$scope
。
- AngularJS,angular.element($0).scope()揭示了每个控制器的函数
- setInterval函数不会更改视图中的$scope
- 函数中的$scope变量值没有更改
- Angularjs从纯JS函数调用$scope.function
- $location.search() - 如何在 $scope.$watch 函数中使用作用域中的参数
- $location.search() 与外部函数一起使用,重置整个$scope.如何避免它
- 在同一工厂中的函数中使用函数并传递$scope(AngularJS)
- ng单击触发函数,但无法更改$scope.var
- 了解窗口调整大小事件与 scope.$apply 与函数$watch
- 将传递的函数参数附加到控制器内的$scope
- angular google maps TypeError:$scope.map.control.refresh不是函数
- 将服务中的函数绑定到$scope(错误:无法设置undefined的属性'onChange')
- scope() 函数在 AngularJS 控制器中返回未定义
- 如何访问ng-init或ng-checked中服务回调函数中声明的$scope变量
- 控制器内部的scope函数不根据视图中的ng模型更新值
- $scope变量从一个函数传递到另一个函数
- AngularJS:何时将变量传递给函数$scope
- 将参数传递给回调函数:Scope issue
- AngularJS-在函数($scope)之前编写$scope的目的
- 在AngularJS应用程序页面加载时执行SharePoint函数($scope issue??)