Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.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/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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变量_Javascript_Angularjs_Jasmine_Jasmine Node - Fatal编程技术网

如何从控制器访问javascript变量

如何从控制器访问javascript变量,javascript,angularjs,jasmine,jasmine-node,Javascript,Angularjs,Jasmine,Jasmine Node,我需要知道如何从控制器访问变量x Javascript 茉莉花 如何从控制器访问变量x 请帮助我将控制器更改为 function myCtrl($scope) { $scope.x =1; } 你不能,x范围是函数,你不能从外部访问它。有几种选择 function myCtrl(){ this.x=1; } 或 然后 或 如果x是私有的,那么不要测试它。只测试公共API 1如果在局部范围内声明变量,则必须 将其作为参数传递给另一个javascript函数。 2在您的情况下,我建议

我需要知道如何从控制器访问变量x

Javascript

茉莉花

如何从控制器访问变量x


请帮助我将控制器更改为

function myCtrl($scope) {
    $scope.x =1;
}
你不能,x范围是函数,你不能从外部访问它。有几种选择

function myCtrl(){
  this.x=1;
}

然后

如果x是私有的,那么不要测试它。只测试公共API

1如果在局部范围内声明变量,则必须 将其作为参数传递给另一个javascript函数。 2在您的情况下,我建议像这样在全球范围内声明“x”:

简单例子

HTML:


参考:

是否有其他方法,不更改控制器文件来访问变量“x”是否有其他方法,不更改控制器文件来访问变量“x”不幸的是,没有。Javascript变量是函数范围。是否有其他方法,不更改控制器文件来访问变量“x”是的,我将变量x声明为全局变量,如何为此编写单元测试。为了访问变量xyes,我将变量x声明为全局变量,不,您没有,在您的问题中,x是局部变量,是myCrl的私有变量。
function myCtrl($scope) {
    $scope.x =1;
}
function myCtrl() {
    var x =1;

}
function myCtrl(){
  this.x=1;
}
function  myCtrl($scope){
   $scope.x=1;
}
describe("myCtrlsettings", function() {


       beforeEach(inject(function($rootScope, $controller) {                           
            this.scope = $rootScope.$new(); 
            this.ctrl =  $controller("myCtrl", {
                  $scope: this.scope
            });
        }));

      it(" Test ", function(){
        expect(this.ctrl.x).toBe(1);  // what should I write here??
      } );
});
describe("myCtrlsettings", function() {


       beforeEach(inject(function($rootScope, $controller) {                           
            this.scope = $rootScope.$new(); 
            this.ctrl =  $controller("myCtrl", {
                  $scope: this.scope
            });
        }));

      it(" Test ", function(){
        expect(this.scope.x).toBe(1);  // what should I write here??
      } );
});
var x;
function myCtrl() {
    x =1;
}
 <section ng-app="myapp" ng-controller="MainCtrl">
      Value of global variable read by AngularJS: {{variable1}}
    </section>
 // global variable outside angular
    var variable1 = true;

    var app = angular.module('myapp', []);

    app.controller('MainCtrl', ['$scope', '$window', function($scope, $window) {
      $scope.variable1 = $window.variable1;
    }]);