Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.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/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
Javascript $scope不返回任何值_Javascript_Angularjs - Fatal编程技术网

Javascript $scope不返回任何值

Javascript $scope不返回任何值,javascript,angularjs,Javascript,Angularjs,我正在尝试访问控制器中的$scope变量。当我控制台$scope时,它会显示值的数量,但当我尝试通过$scope访问它时。它返回未定义的值 我附上了$scope的屏幕截图 在这里,您可以看到它有$id、$parent、templateUrl,但当我试图通过$scope.id、$scope.parent、$scope.templateUrl访问它时,它返回undefined 编辑: brainframeApp.config( ['$interpolateProvider', '$loca

我正在尝试访问控制器中的$scope变量。当我控制台$scope时,它会显示值的数量,但当我尝试通过$scope访问它时。它返回未定义的值

我附上了$scope的屏幕截图

在这里,您可以看到它有$id、$parent、templateUrl,但当我试图通过$scope.id、$scope.parent、$scope.templateUrl访问它时,它返回undefined

编辑:

brainframeApp.config(
    ['$interpolateProvider', '$locationProvider', '$httpProvider', '$routeProvider',
    function($interpolateProvider, $locationProvider, $httpProvider,
    $routeProvider) {
        //configuring angularjs symbos to not to conflict with Django template symbols
        $interpolateProvider.startSymbol('{$');
        $interpolateProvider.endSymbol('$}');
        //$locationProvider.html5Mode(true).hashPrefix('!');
        // setup CSRF support
        $httpProvider.defaults.xsrfCookieName = 'csrftoken';
        $httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
        console.log("TestDavy: App");
        //The route provider
        $routeProvider.
          when('/', {
            templateUrl: '/static/engine/partials/listbrainframes.html',
            controller: 'brainframeCtrl'
          }).
          when('/brainframe/', {
            templateUrl: '/views/post.html',
            controller: 'brainframeCtrlx'
          }).
          when('/brainframe/:id', {
            templateUrl: '/views/post.html',
            controller: 'brainframeCtrlx'
          }).
          otherwise({
            redirectTo: '/'
          });
}]);
brainframeAppControllers.controller('brainframeCtrlx', 
    ['$scope', '$routeParams', function($scope, $routeParams) {
        //console.log($scope.parent);
        //$scope.templateUrl = 'views/post.html?id='+$routeParams.id;
    //console.log($scope);
    $scope.templateUrl = 'views/post.html?id='+$routeParams.id;
    //console.log($scope.templateUrl);
}]);
我正在尝试访问模板url。实际上,我想附加一些带有模板url的参数,以便在后端函数中获取它们

这是我的代码:

brainframeApp.config(
    ['$interpolateProvider', '$locationProvider', '$httpProvider', '$routeProvider',
    function($interpolateProvider, $locationProvider, $httpProvider,
    $routeProvider) {
        //configuring angularjs symbos to not to conflict with Django template symbols
        $interpolateProvider.startSymbol('{$');
        $interpolateProvider.endSymbol('$}');
        //$locationProvider.html5Mode(true).hashPrefix('!');
        // setup CSRF support
        $httpProvider.defaults.xsrfCookieName = 'csrftoken';
        $httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
        console.log("TestDavy: App");
        //The route provider
        $routeProvider.
          when('/', {
            templateUrl: '/static/engine/partials/listbrainframes.html',
            controller: 'brainframeCtrl'
          }).
          when('/brainframe/', {
            templateUrl: '/views/post.html',
            controller: 'brainframeCtrlx'
          }).
          when('/brainframe/:id', {
            templateUrl: '/views/post.html',
            controller: 'brainframeCtrlx'
          }).
          otherwise({
            redirectTo: '/'
          });
}]);
brainframeAppControllers.controller('brainframeCtrlx', 
    ['$scope', '$routeParams', function($scope, $routeParams) {
        //console.log($scope.parent);
        //$scope.templateUrl = 'views/post.html?id='+$routeParams.id;
    //console.log($scope);
    $scope.templateUrl = 'views/post.html?id='+$routeParams.id;
    //console.log($scope.templateUrl);
}]);
我的控制器:

brainframeApp.config(
    ['$interpolateProvider', '$locationProvider', '$httpProvider', '$routeProvider',
    function($interpolateProvider, $locationProvider, $httpProvider,
    $routeProvider) {
        //configuring angularjs symbos to not to conflict with Django template symbols
        $interpolateProvider.startSymbol('{$');
        $interpolateProvider.endSymbol('$}');
        //$locationProvider.html5Mode(true).hashPrefix('!');
        // setup CSRF support
        $httpProvider.defaults.xsrfCookieName = 'csrftoken';
        $httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
        console.log("TestDavy: App");
        //The route provider
        $routeProvider.
          when('/', {
            templateUrl: '/static/engine/partials/listbrainframes.html',
            controller: 'brainframeCtrl'
          }).
          when('/brainframe/', {
            templateUrl: '/views/post.html',
            controller: 'brainframeCtrlx'
          }).
          when('/brainframe/:id', {
            templateUrl: '/views/post.html',
            controller: 'brainframeCtrlx'
          }).
          otherwise({
            redirectTo: '/'
          });
}]);
brainframeAppControllers.controller('brainframeCtrlx', 
    ['$scope', '$routeParams', function($scope, $routeParams) {
        //console.log($scope.parent);
        //$scope.templateUrl = 'views/post.html?id='+$routeParams.id;
    //console.log($scope);
    $scope.templateUrl = 'views/post.html?id='+$routeParams.id;
    //console.log($scope.templateUrl);
}]);

当我在
$scope
上放置console.log时,我只能看到以下属性:

["$$childTail", "$$childHead", "$$nextSibling", "$$watchers", "$$listeners", "$$listenerCount", "$id", "$$ChildScope", "$parent", "$$prevSibling"]
如果要访问控制器内的模板URL:

brainframeApp.run(function($rootScope) {
    $rootScope.$on( "$routeChangeStart", function(event, next, current) {
       $rootScope.templateUrl = next.$$route.templateUrl;
    });
});
现在在控制器中,注入$rootScope并获取模板URL

$rootScope.templateUrl

您“试图以$scope的形式访问它”来自何处?您可以通过$scope.$id和$scope.$parent访问它,但您仍然希望从中获得什么?@squiroid我正在尝试访问模板url。实际上,我想用模板url附加一些参数,这样我就可以在我的后端函数的一般问题中得到它们,假设我有两个控制器,我想将第一个控制器中定义的变量值用在第二个控制器中。可能吗?如果您想在控制器之间共享数据,最好为此创建一个服务/工厂。但是,您也可以使用
$controller('ControllerName',{scope:scope})
共享属性。这是一个很好的例子,因为我提到,他们使用
factory
用于此目的:)我无法使用
Scopes.get()
它表示未定义。作用域没有get函数