刚接触 ngView,我编写了一些与 ngView 示例代码略有不同的代码。通常操作 ngView 的 URL 位于其外部,而在我的代码中,链接位于 ngView 模板内。问题是,当我单击下面的 ShowOrder 链接时,出现 TypeError,函数 $scope.show() 未定义。我在单击链接之前和之后都打印了 $scope 对象。我的嫌疑人被证实了。在点击之前 $scope 已经定义了函数 Show(),之后 $scope 就变成了赤裸裸的,就像是 Angular 刚诞生的一样,没有任何自定义属性。
请帮助我找出我做错的地方,也许稍微了解一下 ngView 是如何在范围内加载和销毁的。谢谢。
这里是主要的 html 代码:
<body ng-app="sampleApp" ng-controller="rootCtrl">
<div ng-view></div>
</body>
应用程序.js:
var sampleApp = angular.module('sampleApp', ['ngRoute']);
sampleApp.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/ShowOrder', {
templateUrl: 'templates/show_order.html',
controller: 'ShowOrderController'
});
}]);
sampleApp.controller('ShowOrderController', function($scope) {
$scope.show = function {console.log("here")}
});
模板代码:
<h2>Show Orders</h2>
<a href="#ShowOrder"> Show Order </a>
{{show()}}
最佳答案
你的说
$scope.show = function {console.log("here")}`
应该是
$scope.show = function() {console.log("here");}
关于javascript - 当 ngView 通过自身内部的链接更改时,我的函数将从 $scope 中剥离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27330147/