Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
angularjs-更换困难“$范围“;加上;这";相似的_Angularjs - Fatal编程技术网

angularjs-更换困难“$范围“;加上;这";相似的

angularjs-更换困难“$范围“;加上;这";相似的,angularjs,Angularjs,根据几个关于这个主题的教程,我试图在我的角度应用程序中稍微减少$scope的混乱。一篇这样的文章展示了清理$scope的一个好策略,可以说,就是让我的控制器成为“类”。像这样, //Don't do this app.controller('MyCtrl', function($scope){ $scope.doStuff = function(){ //Really long function body }; }); //Do this instead var MyCtrl

根据几个关于这个主题的教程,我试图在我的角度应用程序中稍微减少
$scope
的混乱。一篇这样的文章展示了清理
$scope
的一个好策略,可以说,就是让我的控制器成为“类”。像这样,

//Don't do this
app.controller('MyCtrl', function($scope){
  $scope.doStuff = function(){
    //Really long function body
  };
});

//Do this instead
var MyCtrl = function($scope){
  var _this = this;

  _this.doStuff = function(){
    _this.doStuff();
  };
};
我正试图这样做,但遇到了很多麻烦-因为似乎附加到
\u的代码与附加到
$scope
的代码完全不同。比如,

var editor = function($scope){
   var _this = this;

   _this.Model = {
      Id: null,
      Editing: false
   };

   _this.options = {
      columns : [
         { field: "Id", width: 25, title: "Identity" },
         { field: "Name", width: 40, title: "Name" }
      ]
   };
};
然后尝试在指令中使用它

var gridDirective = function($parse) {
   return {
      restrict: 'A',
      scope: true,
      link: function(scope, element, attributes, controller) {
         // I expected this line to output the array I made, but it comes up undefined
         console.log('scope.options: ', scope.options);
      }
   }
};
我希望分配给
\u this
的对象在指令中的
范围中可用,但这没有发生。只有当我直接、明确地将它们分配给
$scope
时,我才会看到它们出现在指令中


有什么解决办法吗?或者我是否必须在任何地方都使用
$scope

$scope
需要设置为
\u this
而不是仅
this
this
在控制器级别对本地var进行设置

快速示例演示:

app.controller('MainController', function(){
  var vm = this;
  vm. error = '';
  vm.onUserComplete = function(response){
    console.log(vm.error);
  };

});
John Papa推荐了一种方法,您可以使用
this
创建控制器方法。然而,我个人确实看到了偏离标准的好处,但如果您想了解更多关于这种方法的信息,请参阅以下文章:


$scope
需要设置为
\u this
,而不是仅将
this
this
设置为控制器级别的本地var

快速示例演示:

app.controller('MainController', function(){
  var vm = this;
  vm. error = '';
  vm.onUserComplete = function(response){
    console.log(vm.error);
  };

});
John Papa推荐了一种方法,您可以使用
this
创建控制器方法。然而,我个人确实看到了偏离标准的好处,但如果您想了解更多关于这种方法的信息,请参阅以下文章:


$scope
需要设置为
\u this
,而不是仅将
this
this
设置为控制器级别的本地var

快速示例演示:

app.controller('MainController', function(){
  var vm = this;
  vm. error = '';
  vm.onUserComplete = function(response){
    console.log(vm.error);
  };

});
John Papa推荐了一种方法,您可以使用
this
创建控制器方法。然而,我个人确实看到了偏离标准的好处,但如果您想了解更多关于这种方法的信息,请参阅以下文章:


$scope
需要设置为
\u this
,而不是仅将
this
this
设置为控制器级别的本地var

快速示例演示:

app.controller('MainController', function(){
  var vm = this;
  vm. error = '';
  vm.onUserComplete = function(response){
    console.log(vm.error);
  };

});
John Papa推荐了一种方法,您可以使用
this
创建控制器方法。然而,我个人确实看到了偏离标准的好处,但如果您想了解更多关于这种方法的信息,请参阅以下文章:


在您的情况下,如果您想使用
this
而不是
$scope
来实现,您应该这样做:

var editor = function($scope){
   var _this = $scope;

   _this.Model = {
      Id: null,
      Editing: false
   };

   _this.options = {
      columns : [
         { field: "Id", width: 25, title: "Identity" },
         { field: "Name", width: 40, title: "Name" }
      ]
   };
};
但这种方法毫无意义。 这篇文章的建议是(在我看来也是)

对于控制器,使用构造函数模式 这是什么意思?这样做的好处是,您将拥有无法分配给作用域的私有功能,您将拥有由您选择分配给作用域的功能,这将使您的控制器更干净。我认为本文中的示例不正确,应该是这样的:

var MyCtrl = function($scope){
  var _this = this;

  $scope.doStuff = _this.doStuff;
};

MyCtrl.prototype.doStuff = function(){
  //Really long function body, also this bounded with scope
};

MyCtrl.prototype.anotherFunc= function(){
  //This is another function which not bounded to scope
};
更新


我建议您在本文中阅读这篇解释angular的styleguide。

在您的案例中,如果您想使用
this
而不是
$scope
,您应该这样做:

var editor = function($scope){
   var _this = $scope;

   _this.Model = {
      Id: null,
      Editing: false
   };

   _this.options = {
      columns : [
         { field: "Id", width: 25, title: "Identity" },
         { field: "Name", width: 40, title: "Name" }
      ]
   };
};
但这种方法毫无意义。 这篇文章的建议是(在我看来也是)

对于控制器,使用构造函数模式 这是什么意思?这样做的好处是,您将拥有无法分配给作用域的私有功能,您将拥有由您选择分配给作用域的功能,这将使您的控制器更干净。我认为本文中的示例不正确,应该是这样的:

var MyCtrl = function($scope){
  var _this = this;

  $scope.doStuff = _this.doStuff;
};

MyCtrl.prototype.doStuff = function(){
  //Really long function body, also this bounded with scope
};

MyCtrl.prototype.anotherFunc= function(){
  //This is another function which not bounded to scope
};
更新


我建议您在本文中阅读这篇解释angular的styleguide。

在您的案例中,如果您想使用
this
而不是
$scope
,您应该这样做:

var editor = function($scope){
   var _this = $scope;

   _this.Model = {
      Id: null,
      Editing: false
   };

   _this.options = {
      columns : [
         { field: "Id", width: 25, title: "Identity" },
         { field: "Name", width: 40, title: "Name" }
      ]
   };
};
但这种方法毫无意义。 这篇文章的建议是(在我看来也是)

对于控制器,使用构造函数模式 这是什么意思?这样做的好处是,您将拥有无法分配给作用域的私有功能,您将拥有由您选择分配给作用域的功能,这将使您的控制器更干净。我认为本文中的示例不正确,应该是这样的:

var MyCtrl = function($scope){
  var _this = this;

  $scope.doStuff = _this.doStuff;
};

MyCtrl.prototype.doStuff = function(){
  //Really long function body, also this bounded with scope
};

MyCtrl.prototype.anotherFunc= function(){
  //This is another function which not bounded to scope
};
更新


我建议您在本文中阅读这篇解释angular的styleguide。

在您的案例中,如果您想使用
this
而不是
$scope
,您应该这样做:

var editor = function($scope){
   var _this = $scope;

   _this.Model = {
      Id: null,
      Editing: false
   };

   _this.options = {
      columns : [
         { field: "Id", width: 25, title: "Identity" },
         { field: "Name", width: 40, title: "Name" }
      ]
   };
};
但这种方法毫无意义。 这篇文章的建议是(在我看来也是)

对于控制器,使用构造函数模式 这是什么意思?这样做的好处是,您将拥有无法分配给作用域的私有功能,您将拥有由您选择分配给作用域的功能,这将使您的控制器更干净。我认为本文中的示例不正确,应该是这样的:

var MyCtrl = function($scope){
  var _this = this;

  $scope.doStuff = _this.doStuff;
};

MyCtrl.prototype.doStuff = function(){
  //Really long function body, also this bounded with scope
};

MyCtrl.prototype.anotherFunc= function(){
  //This is another function which not bounded to scope
};
更新


我建议您在本文中阅读这篇解释angular的styleguide。

您可以在控制器上下文范围内的
ng控制器中提供的别名中访问这些属性。因此,如果您像这样声明控制器:

var editor = function($scope){
   var _this = $scope;

   _this.Model = {
      Id: null,
      Editing: false
   };

   _this.options = {
      columns : [
         { field: "Id", width: 25, title: "Identity" },
         { field: "Name", width: 40, title: "Name" }
      ]
   };
};
ng controller=“EditorController作为编辑器”

这意味着您可以访问编辑器属性