Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.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
Javascript $scope未从控制器注入/继承到带点符号的指令_Javascript_Angularjs_Meanjs - Fatal编程技术网

Javascript $scope未从控制器注入/继承到带点符号的指令

Javascript $scope未从控制器注入/继承到带点符号的指令,javascript,angularjs,meanjs,Javascript,Angularjs,Meanjs,我在将作用域从控制器注入/继承到指令时遇到问题。重要的是,模块并没有分离到它自己的变量中 angular.module('articles').controller('ArticlesController', ['$scope, ... ]).directive('comments', ['$scope' ... //does not work! 不能将作用域作为依赖项注入到指令中。应该是这样的: .directive([function() { return { "

我在将作用域从控制器注入/继承到指令时遇到问题。重要的是,模块并没有分离到它自己的变量中

angular.module('articles').controller('ArticlesController', ['$scope, ...

]).directive('comments', ['$scope' ... //does not work!

不能将作用域作为依赖项注入到指令中。应该是这样的:

.directive([function() {
    return {
        "link": function($scope, $element, $attrs) { 
             //code here
        }
    }
}]);
.directive('myDirective', function() {
  return {
    restrict: 'E',
    scope: {
      myParameter: '=' //this declares directive's interface (2 way binded)
    },
    link: function (scope, element) {
      console.log(scope.myParameter);
    }
  };
});

不能将作用域作为依赖项注入到指令中。应该是这样的:

.directive([function() {
    return {
        "link": function($scope, $element, $attrs) { 
             //code here
        }
    }
}]);
.directive('myDirective', function() {
  return {
    restrict: 'E',
    scope: {
      myParameter: '=' //this declares directive's interface (2 way binded)
    },
    link: function (scope, element) {
      console.log(scope.myParameter);
    }
  };
});

最好的方法是将指令视为黑匣子。您向它提供一些数据,它会更新/显示这些数据。您可以阅读有关指令的所有必需信息,并按如下方式声明指令的输入和输出:

.directive([function() {
    return {
        "link": function($scope, $element, $attrs) { 
             //code here
        }
    }
}]);
.directive('myDirective', function() {
  return {
    restrict: 'E',
    scope: {
      myParameter: '=' //this declares directive's interface (2 way binded)
    },
    link: function (scope, element) {
      console.log(scope.myParameter);
    }
  };
});
然后您可以将此指令用作:

<my-directive my-parameter='variable_declared_in_controller'></my-directive>

最好的方法是将指令视为黑匣子。您向它提供一些数据,它会更新/显示这些数据。您可以阅读有关指令的所有必需信息,并按如下方式声明指令的输入和输出:

.directive([function() {
    return {
        "link": function($scope, $element, $attrs) { 
             //code here
        }
    }
}]);
.directive('myDirective', function() {
  return {
    restrict: 'E',
    scope: {
      myParameter: '=' //this declares directive's interface (2 way binded)
    },
    link: function (scope, element) {
      console.log(scope.myParameter);
    }
  };
});
然后您可以将此指令用作:

<my-directive my-parameter='variable_declared_in_controller'></my-directive>

我刚刚用几个其他作用域完全绕过了
$scope

    .directive('comments', ['$stateParams', '$location', 'Articles',
  function ( $stateParams, $location, Articles) {

   if ($stateParams.articleId != null){

   var article = Articles.get({
        articleId: $stateParams.articleId
      });

   var comments = {articleId: article.articleId, created: Date.now, comment:"Hello, World!", user: "admin" };

       return{
        restrict: "A",
        scope: true,
        template: "<div></div>"
       };
      }
     }
    ]);
.directive('comments'、['$stateParams'、'$location'、'Articles',
函数($stateparms、$location、Articles){
如果($stateParams.articleId!=null){
var article=Articles.get({
articleId:$stateParams.articleId
});
var comments={articleId:article.articleId,created:Date.now,注释:“Hello,World!”,用户:“admin”};
返回{
限制:“A”,
范围:正确,
模板:“”
};
}
}
]);

我刚刚用几个其他作用域完全绕过了
$scope

    .directive('comments', ['$stateParams', '$location', 'Articles',
  function ( $stateParams, $location, Articles) {

   if ($stateParams.articleId != null){

   var article = Articles.get({
        articleId: $stateParams.articleId
      });

   var comments = {articleId: article.articleId, created: Date.now, comment:"Hello, World!", user: "admin" };

       return{
        restrict: "A",
        scope: true,
        template: "<div></div>"
       };
      }
     }
    ]);
.directive('comments'、['$stateParams'、'$location'、'Articles',
函数($stateparms、$location、Articles){
如果($stateParams.articleId!=null){
var article=Articles.get({
articleId:$stateParams.articleId
});
var comments={articleId:article.articleId,created:Date.now,注释:“Hello,World!”,用户:“admin”};
返回{
限制:“A”,
范围:正确,
模板:“”
};
}
}
]);


这与$complie和include类似吗?我也试过了。让我试试这个。编辑:Get参数'ArticlesController'不是函数,Get undefinedWhere获得该错误?您的代码现在看起来像什么?我在尝试将
$scope
注入指令时遇到了这个错误。我的代码现在看起来像我对这个问题的回答。这类似于$complie和include吗?我也试过了。让我试试这个。编辑:Get参数'ArticlesController'不是函数,Get undefinedWhere获得该错误?您的代码现在看起来像什么?我在尝试将
$scope
注入指令时遇到了这个错误。我的代码现在看起来像是我对这个问题的回答。是的,我可能需要添加另一个控制器,因为我需要获取更多的数据(文章注释),而我现在只有静态JSON。是的,我可能需要添加另一个控制器,因为我需要获取更多的数据(文章注释)我现在只使用静态JSON。
$scope
不是一个单例。它被
$compile
作为本地变量注入控制器,并且作为指令链接函数的第一个参数可用。它在指令构造函数中不可用于注入。但是,您可以插入单例提供程序:
$rootScope
$http
$timeout
$q
$document
,等等。感谢您的解释。为什么它们允许
$rootScope
(可能存在误用)而不允许
$scope
$rootScope
可以用于使用
$on
注册侦听器,并使用
$broadcast
$emit
发送事件。有关更多信息,请参阅。
$scope
是由
$compile
注入的本地文件。有关控制器局部变量的更多信息,请参见
$scope
不是单例。它被
$compile
作为本地变量注入控制器,并且作为指令链接函数的第一个参数可用。它在指令构造函数中不可用于注入。但是,您可以插入单例提供程序:
$rootScope
$http
$timeout
$q
$document
,等等。感谢您的解释。为什么它们允许
$rootScope
(可能存在误用)而不允许
$scope
$rootScope
可以用于使用
$on
注册侦听器,并使用
$broadcast
$emit
发送事件。有关更多信息,请参阅。
$scope
是由
$compile
注入的本地文件。有关控制器局部变量的更多信息,请参见“是”,您可以在指令构造函数中插入除
$scope
之外的几乎任何内容。使用
scope:true
指令将获得一个从其父作用域继承的新作用域。无论如何,在我的例子中,
$scope
没有被继承。与false相同(假定为OG
$scope
)如果指令没有父作用域,则它继承自
$rootScope
。是的,您可以在指令构造函数中插入除
$scope
之外的几乎任何内容。使用
scope:true
指令将获得一个从其父作用域继承的新作用域。无论如何,在我的例子中,
$scope
没有被继承。与false相同(假定为OG
$scope
)如果指令没有父作用域,则它继承自
$rootScope