2

我有一个模型,我希望它是可编辑的,但由于某种原因没有任何改变,文本框没有显示,并且模型在使用ng-view.

我可以看到enableEditor()使用console.log.

如果我在没有profile.htmlng-view的情况下内联而不是在index.html中编写它,一切都会完美运行。

这里是文件:

应用程序.js

var proGamersApp = angular.module('proGamersApp', ['ngResource']).
    config(function ($routeProvider) {

        $routeProvider.
        when('/', { controller: 'ProfileController', templateUrl: '/app/partials/profile.html' }).
        otherwise({ redirectTo: '/' });
    });

var ProfileController = function ($scope) {

    $scope.init = function () {
        $scope.title = 'first title';
    };

    $scope.init();
    $scope.enableEditor = function () {
        console.log('enableEditor()')
        $scope.editorEnabled = true;
        $scope.editableTitle = 'second title';
        $scope.title = 'second title';
    };

     ...
};

索引.html

<!doctype html>
<html ng-app="proGamersApp">
<head>
    <title>Pro Gamers</title>

    <!-- Scripts -->
    <script src="/app/lib/angular.min.js"></script>
    <script src="/app/lib/angular-resource.js"></script>
    <script src="/app/app.js"></script>

</head>
<body>
    <div ng-view></div>
</body>
</html>

profile.html

 <div ng-hide="editorEnabled">
        {{title}}
      <a href="#" ng-click="enableEditor()">Edit title</a>
    </div>
    <div ng-show="editorEnabled">
        <input ng-model="title" ng-show="editorEnabled">
        <a href="#" ng-click="save()">Save</a>
        or
      <a href="#" ng-click="disableEditor()">cancel</a>.
    </div>

有人知道我做错了什么吗?

谢谢

4

1 回答 1

1

该链接正在添加到您的地址,导致路由器刷新页面并破坏您所有的 $scope 变量。不要使用空白锚,而是使用像锚一样样式的跨度:

span:hover {
    cursor:pointer;
}

这只会给光标指针,根据需要自定义颜色。根据您的评论,不要将其添加target=_self到href,然后添加:

<a href="#" target=_self ng-click="save()">Save</a> //prevent address bar change

正如我之前所说,使用跨度代替。

于 2013-09-06T15:29:45.023 回答