我是Angular的初学者(版本1.6.3),我遇到了这个问题:
我有一个名为prof的控制器:
(function () {
'use strict';
angular
.module('app.prof')
.config(config);
/** @ngInject */
function config($stateProvider, $translatePartialLoaderProvider, msApiProvider) {
// State
$stateProvider
.state('app.prof', {
url : '/prof',
views : {
'content@app': {
templateUrl: 'app/main/prof/prof.html',
controller : 'ProfController as vm'
}
},
resolve : {
Data : function(msApi){
return msApi.resolve('data@get');
}
}
});
$translatePartialLoaderProvider.addPart('app/main/prof');
msApiProvider.register('data', ['app/data/prof/prof-data.json']);
}
})();
以下是其相关模块:
<div class="document" ng-repeat="document in vm.documents">
<ms-card template="'app/custom-directives/prof-card/prof-card.html'"
ng-model="document"></ms-card>
</div>
这是主要问题:我有这个HTML:
<ms-card>
它工作得很好,数据被正确绑定所有,但是当我把模板直接调用到页面中而不是通过console.log()
调用它时它就不再起作用了!
我试图把一些ng-repeat
放在一个小地方,但它总是说数据没有定义;我不懂。
另外,<md-list-item class="document-item md-white-bg md-2-line" md-ink-ripple>
<div class="media ml-16">
<img class="image-apercu" ng-src="{{card.media.image.src}}" alt="{{card.media.image.alt}}" ng-show="card.media.image">
</div>
始终正常
编辑:我调用的一些html:
ng-model="document"
ps:当我直接添加html时,我不要忘记放Shans-Macbook:~ Shanshan$ sudo cp /usr/local/mysql/support-files/my-small.cnf /etc/my.cnf
Password:
cp: /usr/local/mysql/support-files/my-small.cnf: No such file or directory
Shans-Macbook:~ Shanshan$ sudo cd /usr/local/mysql/support-files/my-small.cnf /etc/my.cnf
/usr/bin/cd: line 4: cd: /usr/local/mysql/support-files/my-small.cnf: No such file or directory
Shans-Macbook:~ Shanshan$ sudo /usr/local/mysql/support-files/mysql.server start
Starting MySQL
.................................................................................................... ERROR! The server quit without updating PID file (/usr/local/mysql/data/Shans-Macbook.pid).
Shans-Macbook:~ Shanshan$ mysqld_safe --skip-grant-tables &
[1] 15852
Shans-Macbook:~ Shanshan$ 2017-03-31T07:16:22.6NZ mysqld_safe Logging to '/usr/local/var/mysql/Shans-Macbook.err'.
2017-03-31T07:16:22.6NZ mysqld_safe Logging to '/usr/local/var/mysql/Shans-Macbook.err'.
/usr/local/bin/mysqld_safe: line 139: /usr/local/var/mysql/Shans-Macbook.err: Permission denied
2017-03-31T07:16:22.6NZ mysqld_safe Starting mysqld daemon with databases from /usr/local/var/mysql
/usr/local/bin/mysqld_safe: line 139: /usr/local/var/mysql/Shans-Macbook.err: Permission denied
/usr/local/bin/mysqld_safe: line 193: /usr/local/var/mysql/Shans-Macbook.err: Permission denied
2017-03-31T07:16:22.6NZ mysqld_safe mysqld from pid file /usr/local/var/mysql/Shans-Macbook.pid ended
/usr/local/bin/mysqld_safe: line 139: /usr/local/var/mysql/Shans-Macbook.err: Permission denied
Shans-Macbook:~ Shanshan$ mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
[1]+ Done mysqld_safe --skip-grant-tables
Shans-Macbook:~ Shanshan$ mysql.server start
Starting MySQL
./usr/local/Cellar/mysql/5.7.17/bin/mysqld_safe: line 139:
/usr/local/var/mysql/Shans-Macbook.err: Permission denied
/usr/local/Cellar/mysql/5.7.17/bin/mysqld_safe: line 139:
/usr/local/var/mysql/Shans-Macbook.err: Permission denied
/usr/local/Cellar/mysql/5.7.17/bin/mysqld_safe: line 193:
/usr/local/var/mysql/Shans-Macbook.err: Permission denied
/usr/local/Cellar/mysql/5.7.17/bin/mysqld_safe: line 139:
/usr/local/var/mysql/Shans-Macbook.err: Permission denied
ERROR! The server quit without updating PID file (/usr/local/var/mysql/Shans-Macbook.pid).
但它仍然不起作用
这对我来说非常困惑:/
答案 0 :(得分:1)
您无法将“vm”对象与“$ scope”绑定。你需要用Scope绑定它,如
function ProfController($scope, Data)
{
//$scope.vm = this;
$scope.vm.documents = Data.documents;
$scope.vm.classes = Data.classes;
}
你的Html应该是这样的:
<body ng-app="app.prof" ng-controller="ProfController">
<div class="document" ng-repeat="document in vm.documents">
<ms-card template="'app/custom-directives/prof-card/prof-card.html'"
ng-bind="document"></ms-card>
</div>
</body>
希望这对你有用
答案 1 :(得分:0)
最后我找到了自己 - 我把控制器放回原处,我将所有{{card.something}}
重命名为{{document.something}}
,我删除了ng-bind=""
中的<div>
那是在循环。