Angular.js:为什么用一个简单的辅助函数改变 $scope 会导致未插入的 {{...}} 表达式?
Angular.js: Why does mutating $scope with a simple helper function result in uninterpolated {{...}} expressions?
好的,这是一个 git 差异:
...
function makeSomeCtrl() {
+ var identity = function (x) {return x;};
+ $scope = identity($scope)
return function($scope) {
...
突然之间,页面“{{page.hello}}”中的所有 angular 而不是“Hello, World”
到底为什么...?
编辑: 这是一个演示此行为的 Plunker:http://plnkr.co/edit/lztwTNdAN4baVDtFLCX4?p=preview
分辨率
因此,$scope
不在范围内。哦!这既元又令人尴尬。
再次编辑:未解决
Plunker 中的代码不 反映了我试图调试的原始代码。即,原始代码的问题不是范围错误。这是我在起草 Plunker 时犯的一个错误。这是一个更新的 Plunker,它更准确地反映了我正在处理的代码:http://plnkr.co/edit/iswQq2qqRyQjsPA4Bkk6?p=preview
(function() {
'use strict';
function addSomeValue(obj, x) {
obj.value = x;
}
function makeCtrl(x) {
function Ctrl($scope){
addSomeValue($scope, x)
}
return Ctrl;
}
angular.module('modal-example', [])
.controller('Ctrl', ['$scope', makeCtrl("Hello!")]);
}());
在呈现的页面中产生漂亮的胖“{{value}}”。
您无法在 makeCtrl
中访问 $scope
,因为 Angular 不会将其注入到您的函数中。如果将行 $scope = identity($scope)
移动到 RandomIdentityCtrl
,您的代码可以正常工作。
这很简单,$scope
在你尝试调用它之前没有定义。
function makeCtrl(someVar) {
function identity(o){return o;}
//Where did I come from?
$scope = identity($scope)
function RandomIdentityCtrl($scope){
$scope.beforeIdentity = "Before Identity";
$scope.afterIdentity = "After Identity";
}
return RandomIdentityCtrl;
}
如果你只是将它移到构造函数中,你会发现它工作得很好:
function makeCtrl(someVar) {
function identity(o){return o;}
function RandomIdentityCtrl($scope){
$scope.beforeIdentity = "Before Identity";
//Now I work! Yay!
$scope = identity($scope)
$scope.afterIdentity = "After Identity";
}
return RandomIdentityCtrl;
}
更新
在此处查看更新后:http://plnkr.co/edit/iswQq2qqRyQjsPA4Bkk6?p=preview
看来您只是打错了几个字。
一个在你的函数中:
function makeCtrl(x) {
function Ctrl($scope){
addSomeValue($scope, x)
}
//Should be 'Ctrl'
return RandomIdentityCtrl;
}
还有一个在您的 HTML 模板中:
<!-- Should be "Ctrl" -->
<div class="container" ng-controller="RandomIdentityCtrl">
再次修复这些会导致所需的行为:http://plnkr.co/edit/eKdDPYylTCS1czwKKWew?p=preview
好的,这是一个 git 差异:
...
function makeSomeCtrl() {
+ var identity = function (x) {return x;};
+ $scope = identity($scope)
return function($scope) {
...
突然之间,页面“{{page.hello}}”中的所有 angular 而不是“Hello, World”
到底为什么...?
编辑: 这是一个演示此行为的 Plunker:http://plnkr.co/edit/lztwTNdAN4baVDtFLCX4?p=preview
分辨率
因此,$scope
不在范围内。哦!这既元又令人尴尬。
再次编辑:未解决
Plunker 中的代码不 反映了我试图调试的原始代码。即,原始代码的问题不是范围错误。这是我在起草 Plunker 时犯的一个错误。这是一个更新的 Plunker,它更准确地反映了我正在处理的代码:http://plnkr.co/edit/iswQq2qqRyQjsPA4Bkk6?p=preview
(function() {
'use strict';
function addSomeValue(obj, x) {
obj.value = x;
}
function makeCtrl(x) {
function Ctrl($scope){
addSomeValue($scope, x)
}
return Ctrl;
}
angular.module('modal-example', [])
.controller('Ctrl', ['$scope', makeCtrl("Hello!")]);
}());
在呈现的页面中产生漂亮的胖“{{value}}”。
您无法在 makeCtrl
中访问 $scope
,因为 Angular 不会将其注入到您的函数中。如果将行 $scope = identity($scope)
移动到 RandomIdentityCtrl
,您的代码可以正常工作。
这很简单,$scope
在你尝试调用它之前没有定义。
function makeCtrl(someVar) {
function identity(o){return o;}
//Where did I come from?
$scope = identity($scope)
function RandomIdentityCtrl($scope){
$scope.beforeIdentity = "Before Identity";
$scope.afterIdentity = "After Identity";
}
return RandomIdentityCtrl;
}
如果你只是将它移到构造函数中,你会发现它工作得很好:
function makeCtrl(someVar) {
function identity(o){return o;}
function RandomIdentityCtrl($scope){
$scope.beforeIdentity = "Before Identity";
//Now I work! Yay!
$scope = identity($scope)
$scope.afterIdentity = "After Identity";
}
return RandomIdentityCtrl;
}
更新
在此处查看更新后:http://plnkr.co/edit/iswQq2qqRyQjsPA4Bkk6?p=preview
看来您只是打错了几个字。
一个在你的函数中:
function makeCtrl(x) {
function Ctrl($scope){
addSomeValue($scope, x)
}
//Should be 'Ctrl'
return RandomIdentityCtrl;
}
还有一个在您的 HTML 模板中:
<!-- Should be "Ctrl" -->
<div class="container" ng-controller="RandomIdentityCtrl">
再次修复这些会导致所需的行为:http://plnkr.co/edit/eKdDPYylTCS1czwKKWew?p=preview